File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors." Watch "Constructors." New topic


Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Query: What is the use of declaring a constructor as protected, when we know that the constructors are not inherited.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Protected constuctors can be called by subclasses and also classes within the same package.

agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Although constructors are not inherited yet they aare called implicitly by run time system or explicitly by U
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1599
This seems tricky. Guess, if I have a "private" constructor and inherit a subclass from it. Now, when I would instantiate a sub-class sub-class constructor will be called which will in-turn call super class constructor, but is that accessible???

Shows error. Good... thank you all.

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 980

As akhil had mentioned in his post...

We cannot instantiate the class with only private constructor..

Such class object can be created only in that same class...

So in the event of inheriting this cannot instantiate an object of the sub class...since the base class constructor is not visible to the sub class..

If it is protected is visible in any other class withing this package and any other class that extends this base class...

So we can instantiate an object of this sub class..

But if we want to instantiate an object of the base class elsewhere a different behaves as a private scope...whether you are extending that class or not...

In case you are can cannot create directly the base class.. instance...but you can create instance of the extended class.. in this extended class..

[ October 05, 2005: Message edited by: A Kumar ]
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Thanks to kumar & Akhil.
I agree. Here's the link:
subject: Constructors.
jQuery in Action, 3rd edition