• 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

Can we extends a class having private constructor?

 
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys!

I need conclusion , in the case of parent class which having private default constructor and i'm trying to inherit parent class into child class like this....



when i'm compiling above code then getting compilation error saying Demo() has private access in Demo.
and also i tried many ways by arranging above code but i did't get success,so here my doubt is that
1-can we inherit a Parent class having private constructor ,inside Child class if we can then in which case please explain with a sample code.
2-if above is ok then can it possible to access Parent class private constructor from Child class??
I'm in doubt in this case please help me by posting yours advantageous reply.

with respect,
vivek
 
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is you can't extend the Parent class if it has a private default constructor. You have to make the constructor available to the subclass. In this case you need to have a default constructor that have a protected or public or default access modifier. A class that have a private constructor is usually use as a utility class for example, or a class that work as a singleton.
 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I Wayan Saryada wrote:The answer is you can't extend the Parent class if it has a private default constructor.



Thanks alot Wayan,
for your quick reply , here i have one more question to you, can we never extends Parent class if it's constructor has declared once as private?? in any condition??
waiting for your reply..

 
Wayan Saryada
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, I didn't get your question. Can you rephrase it?
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vivek Bhardwaj wrote:

I Wayan Saryada wrote:The answer is you can't extend the Parent class if it has a private default constructor.



Thanks alot Wayan,
for your quick reply , here i have one more question to you, can we never extends Parent class if it's constructor has declared once as private?? in any condition??
waiting for your reply..


If a class is having only one constructor, and that is private, then you won't be able to extend that class.
 
Ranch Hand
Posts: 83
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I Wayan Saryada wrote:The answer is you can't extend the Parent class if it has a private default constructor.


Technically, you can extend a class that has a private constructor if the subclass is a nested class.

 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike

In the example you have given, how would you create an instance of Subclass outside of Superclass? You would need an instance of Superclass first which you cannot achieve (as it has no public constructor).
 
Mike Okri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:Mike

In the example you have given, how would you create an instance of Subclass outside of Superclass? You would need an instance of Superclass first which you cannot achieve (as it has no public constructor).



I'm saying that technically, it's not impossible to extend a class that has a private constructor. If a nested Subclass extends a Superclass with a private constructor, Subclass cannot be instantiated outside Superclass.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Okri wrote:If a nested Subclass extends a Superclass with a private constructor, Subclass cannot be instantiated outside Superclass.


It might be possible with reflection and the setAccessible method.
 
Mike Okri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Mike Okri wrote:If a nested Subclass extends a Superclass with a private constructor, Subclass cannot be instantiated outside Superclass.


It might be possible with reflection and the setAccessible method.



Oh yes. That's right.
 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Okri wrote:

I Wayan Saryada wrote:The answer is you can't extend the Parent class if it has a private default constructor.


Technically, you can extend a class that has a private constructor if the subclass is a nested class.



your answer helped me to clear my doubt, thank you so much Mike, in the case of nested classes, i'm a bit confuse actually i have't covered the topic yet, about to cover that topic,after that i'll take a look on your code, if there will be any doubt then i'd like to question to you.
well, thanks to all Guys! I'm very happy,learning well from here all of you guys......so really thank you so much to all of you Guys.

Regards
Vivek
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:Mike

In the example you have given, how would you create an instance of Subclass outside of Superclass? You would need an instance of Superclass first which you cannot achieve (as it has no public constructor).


You just have to provide a method for passing generating or passing an instance of Superclass out: example is a getInstance() method. Then someone could do Superclass.getInstance().new Subclass().

Of course, another way around it would be to have a second, non-private constructor that takes a parameter. The subclass would have to explicitly call this second constructor:
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps a better question is "should we extend a class having only a private constructor"

Assuming that the class in question had been carefully engineered, the answer must be "no" even if there are hacks that can get around it.

When writing a class, you have to explicitly change the access attributes to have a private constructor. You should give the designer some credit (if its due) that they made this change on purpose.

Now, when this happens, I'd like to see that the class is marked 'final' so the author's intent is clear. But the private constructor must be a deliberate act.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic