• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extending class with private constructor.  RSS feed

 
Nimish Patel
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • 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
 
Chidanand Chauhan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

As they say in C++, friends can touch your privates...
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Posts: 22507
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did I say that?


I'm sorry. I meant I agree with you when you said Yes.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 2409
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!