File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

Sanjay Izardar
Greenhorn

Joined: Apr 18, 2011
Posts: 19
what is the use of protected constructor?
what is the default accessibility of a a constructor, how can we check it?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Welcome to the Ranch, Sanjay!

The accessibility keywords have the same meaning for constructors as they have for regular methods and member variables. So, a protected constructor can be accessed only by subclasses or by classes in the same package.

Sometimes you might want to make a constructor protected to make sure that it can be called only from subclasses, so that the class itself can't be instantiated directly. Often this is appropriate for abstract classes. The fact that the class is abstract already prevents you from instantiating it directly, but making the constructors protected emphasizes this, so that it's more clear for programmers who are going to use your class later.

The default accessibility of a constructor is exactly the same as the default accessibility of regular methods and member variables: if you don't specify public, protected or private, the constructor is only visible in the class itself and in classes in the same package.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sanjay Izardar
Greenhorn

Joined: Apr 18, 2011
Posts: 19
thank you Jesper.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor
 
Similar Threads
super() call in inner classes
constructors...
constructor
abstract class
what is the invisible statement in the constructor