• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we extends a class having private constructor?

 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Wayan Saryada
Ranch Hand
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 105
IntelliJ IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, I didn't get your question. Can you rephrase it?
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Mike Okri
Ranch Hand
Posts: 83
  • 1
  • Mark post as helpful
  • send pies
  • 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.

 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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:
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic