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 Whats is the use of making a Constructor Protected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Whats is the use of making a Constructor Protected" Watch "Whats is the use of making a Constructor Protected" New topic
Author

Whats is the use of making a Constructor Protected

Haricharan Ramachandra
Greenhorn

Joined: Feb 16, 2004
Posts: 28
Hi

Can anyone please let me know what is the use of making a Constructor Protected. I know that if we make a Constructor private you can't instanciate that class. But if i make it Public or Protected i can still instanciate it. So what is the difference between maing a Constructor Protected instead of Public

thanks for your help in advance

thanks
Hari


Haricharan Ramachanra<br />SCJP 1.4<br />javaworks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

If a constructor is protected, then (outside of the package where it is defined) it can only be used by a subclass -- i.e., in a super() call.


[Jess in Action][AskingGoodQuestions]
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
I know that if we make a Constructor private you can't instanciate that class.

Sure you can. A public method in a class can access private members--including private methods.

But if i make it Public or Protected i can still instanciate it. So what is the difference between maing a Constructor Protected instead of Public

A class that is not in the same package as a class that has a protected constructor cannot create objects of that class (that is assuming there is no helper method like in the above example). Or, in other words, if a class has a protected constructor, then only classes in the same package can create objects of that class.

On the other hand, if a class has a public constructor, then any class can create objects of that class.
[ October 27, 2006: Message edited by: sven studde ]
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
I have a question that came to mind when thinking about the op's question: is there any effect in declaring a method protected if the method is not a constructor or a static method? It seems to me that if the method isn't a constructor or a static method, then it will only be called with an object of the class, and therefore protected has no meaning for such a method.

Ok, I just realized there are inheritance implications: a protected method will be inherited by a subclass unlike private methods, which aren't inherited. So, that means you can override the method in the subclass and therefore polymorphism will work.
[ October 27, 2006: Message edited by: sven studde ]
 
jQuery in Action, 2nd edition
 
subject: Whats is the use of making a Constructor Protected
 
Similar Threads
singleton and force to use it
constructors
Primary Key Class
constructor
protected constructor in Singleton class