File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please Help Me To Clear My Doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please Help Me To Clear My Doubt?" Watch "Please Help Me To Clear My Doubt?" New topic
Author

Please Help Me To Clear My Doubt?

V. Potluri
Ranch Hand

Joined: Mar 29, 2007
Posts: 36
Hi,

Under what circumstances, we can declare constructor of a class as a protected?



According to my knowledge, constructor never participate in inheritance, then under what circumstances we have to declare constructor as protected?

Thanks in advance,

Regards,
Gopal
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
I am not sure , but If you try to access the class from different package , the class will be accessible but it will not create the object as constructor declared is protected.

check for this condition.


My Techie Blog - Java Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

According to my knowledge, constructor never participate in inheritance, then under what circumstances we have to declare constructor as protected?


What do you mean by "never participate in inheritance"? While it is true that a constructor can't be overridden, but it can be called from the subclass. Hence, you need to define access to it -- be it from the subclass or any class that uses it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

What do you mean by this

under what circumstances we have to declare constructor as protected?


You are not forced to do so...

however, if you declare all the constructors of a class as protected, then you can sub-class it in different packages, but you cannot create instances of the class outside the package....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
V.G. Hindriksen
Greenhorn

Joined: Sep 15, 2008
Posts: 9
When this can be used?
I think in the case you *prefer* it to be subclassed, so abstract is too much. Think about a colleague who hates almost empty subclasses and does not want to extend your abstract Animal-class; you change it this way, explaining public was too much with good reasons, and go on with your work.

Probably you'll only use private and default and public. Java-engineers did not make the language on usage-statistics, but only left parts out which could lead to problems (such as multiple extension). So the question is the reverse: why wouldn't it be possible to make a class-instantiation protected?
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
if the constructor of a class is declared as protected, objects of that class cannot be created outside its package.
And constructors will play a role in inheritance also. Only thing is that constructors are not inherited. A call to the super class constructor will be added in the constructor of the subclass by the compiler if the default constructor is there otherwise you will have to add the same.(ofcourse if there is a call to this(), call to super class constructor is not required.)


Thanks,<br />Srilatha M
Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
One thing added to SRILATHA

A call to the super class constructor will be added in the constructor of the subclass by the compiler if the default constructor is there otherwise you will have to add the same.(ofcourse if there is a call to this(), call to super class constructor is not required.)


The compiler will add a call to the default constructor of the superclass, when it finds a constructor. ie, it will add a super(), as the first line of the constructor.

when a call to this() is given, the compiler will go for the default constructor of the same class. since it can't contain a call to this() again(Recursive Contructor invocation error), the first line added will be super(). so it will perform the functionality implicitly.


Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
V. Potluri
Ranch Hand

Joined: Mar 29, 2007
Posts: 36
Hi,

Thanks a lot for each of you for spending your precious time for clearing my doubt.

Have a wonderful day ahead!

Regards,
Gopal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Help Me To Clear My Doubt?