• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extending class with private constructor.

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I faced this question in an interview for which I have not yet received an answer.

The question is: Is it possible to extend a class that has got only private constructor? If yes then how?


Thanks in advance,

Nimish Patel
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nimish Patel:
The question is: Is it possible to extend a class that has got only private constructor? If yes then how?



No, Once you declare constructor as private then you wouldn't be able to inherit the class. This is another way to prevent it from inhertance(other way is Final Keyword).

Thanks
Chidanand
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.

As they say in C++, friends can touch your privates...
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Jeff. The wording states "is it possible". The answer is that yes it is possible in some cases because you didn't specify the type of constructor in the superclass that is private.

If you make the no-argument constructor in the superclass private, and you have a constructor in the subclass, then you won't be able to compile the subclass because the compiler can't make an implicit or explicit call to the no-argument constructor of the superclass.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And likewise even if B only has a private constructor, A can still create instances of it.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:

If you make the no-argument constructor in the superclass private, and you have a constructor in the subclass, then you won't be able to compile the subclass because the compiler can't make an implicit or explicit call to the no-argument constructor of the superclass.



Did I say that?


I encorporated DrClap's point that B's constructor could be private too -- it's that two-way friendship of a nested class.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Did I say that?



I'm sorry. I meant I agree with you when you said Yes.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
If you make the no-argument constructor in the superclass private, and you have a constructor in the subclass, then you won't be able to compile the subclass because the compiler can't make an implicit or explicit call to the no-argument constructor of the superclass.



If you make a different constructor private, the class won't have a no-arg constructor, so a subclass wouldn't be able to call it either.

The type of the constructor that is declared private really doesn't matter - as long as there is not also a non-private constructor declared, only nested classes can extend the class.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to add one more log to the fire: you don't have to write a constructor for the subclass for this to be a problem, either. The subclass gets a default constructor if you don't write one, and that constructor still needs to call the superclass's default constructor. So if a superclass has only inaccessible constructors, then a subclass won't compile whether you explicitly write a constructor or not.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:


If you make a different constructor private, the class won't have a no-arg constructor, so a subclass wouldn't be able to call it either.

The type of the constructor that is declared private really doesn't matter - as long as there is not also a non-private constructor declared, only nested classes can extend the class.



Point taken. I misinterpreted part of the original posts.
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic