Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors.

 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Query: What is the use of declaring a constructor as protected, when we know that the constructors are not inherited.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Protected constuctors can be called by subclasses and also classes within the same package.
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although constructors are not inherited yet they aare called implicitly by run time system or explicitly by U
 
Akhilesh Trivedi
Ranch Hand
Posts: 1603
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to kumar & Akhil.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic