File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can we extends a class having private constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we extends a class having private constructor?" Watch "Can we extends a class having private constructor?" New topic
Author

Can we extends a class having private constructor?

viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

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

Joined: Feb 05, 2004
Posts: 105

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.


Website: Learn Java by Examples
viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

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

Joined: Feb 05, 2004
Posts: 105

I am sorry, I didn't get your question. Can you rephrase it?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Mike Okri
Ranch Hand

Joined: Jun 22, 2011
Posts: 83
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

Joined: Nov 09, 2011
Posts: 1031
    
    5

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

Joined: Jun 22, 2011
Posts: 83
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

Joined: Aug 05, 2005
Posts: 3742
    
  16
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.


Joanne
Mike Okri
Ranch Hand

Joined: Jun 22, 2011
Posts: 83
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

Joined: Nov 07, 2011
Posts: 74

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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:


Steve
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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
 
subject: Can we extends a class having private constructor?