• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Constructors.

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Protected constuctors can be called by subclasses and also classes within the same package.
 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although constructors are not inherited yet they aare called implicitly by run time system or explicitly by U
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to kumar & Akhil.
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic