jQuery in Action, 2nd edition*
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
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: 14338
    
  22

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 8 API documentation
Sanjay Izardar
Greenhorn

Joined: Apr 18, 2011
Posts: 19
thank you Jesper.
 
jQuery in Action, 2nd edition
 
subject: Constructor