This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors." Watch "Constructors." New topic
Author

Constructors.

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: 8904

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


Groovy
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: 1526
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: 979
Hi,

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 class...you cannot instantiate an object of the sub class...since the base class constructor is not visible to the sub class..

If it is protected ....it 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 ...in a different package..it behaves as a private scope...whether you are extending that class or not...

In case you are extending...you 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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Constructors.
 
Similar Threads
Constructors are not inhertied - True
Can constructor throw exception?
Why I can't call the parent class constructor?
another question on constructors
OOP 4: Lookup