• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How can we extend a class having only a private constructor?

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

I want to know how can we extend a class having only a private constructor? I know its impossible but I mean how can we get around it?
thanks friends.
 
Ranch Hand
Posts: 40
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class has private constructor to prevent is instance creation from outside the class.
If anyhow, the class with private constructor is extended, then when subclass instance is created, it will implicitly instantiate super class also, which is wrong.
Hence we cannot extends a super class unless it has at least one non-private constructor even though its not 0-arg constructor.
 
Saloon Keeper
Posts: 14318
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it's impossible.

Use composition instead. If the class you want to extend implements some interface, your class can implement that interface as well, and accept an instance of the 'super class' in the constructor. Forward calls to this instance where necessary.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well, technically, a class with a private constructor can be extended by an inner class that has the same top level class as that class.

Of course, if that class can be modified to add an inner class, then it would probably be easier to just make the constructor not be private...

Henry
 
Bartender
Posts: 5069
189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One easy way around would be not to extend that class as such,
but to have an instance of that class as a public member.
In the constructor, you get an instance of that class,
and you then have to use something like:
b.a.originalAmethod();

You do not have to override all A's public methods.
If that is practical, I don't know.
 
hani se
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Well, technically, a class with a private constructor can be extended by an inner class that has the same top level class as that class.

Of course, if that class can be modified to add an inner class, then it would probably be easier to just make the constructor not be private...

Henry



Thank you Henry,
I know it, but i want to know is there another way or not? have you ever heard about Byte Code Generation Library? i think it can help
 
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . an instance of that class as a public member.
. . .
You do not have to override all A's public methods.
. . .

A public member And which class is A? Not only does that show how confusing names too short can be, but also I haven't seen any sign of A earlier in this thread.
 
Campbell Ritchie
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hani se wrote:. . . have you ever heard about Byte Code Generation Library? i think it can help

Did you really mean help? I would have thought there is a reason for that class not having an accessible constructor and trying to circumvent it would constitute bad programming.
 
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hani se wrote:I know its impossible but I mean how can we get around it?


So you know it is impossible, but you don't believe it's impossible? You're basically saying "I know I can't flap my arms and fly, but how can I flap my arms and fly?"

I think there is something else going on here you are not telling us. I don't know anything about "Bytecode Generation Library", but it sounds like you want to hack the bytecode to change things around to do something the language won't let you do. You are no longer programming in Java if you do this, IMHO.
 
Piet Souris
Bartender
Posts: 5069
189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Piet Souris wrote:. . . an instance of that class as a public member.
. . .
You do not have to override all A's public methods.
. . .

A public member


Yep, like in System.out.

Campbell Ritchie wrote:
And which class is A? Not only does that show how confusing names too short can be, but also I haven't seen any sign of A earlier in this thread.


That was only a schematic notation. I assume the context is clear.
 
Campbell Ritchie
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Afraid the context was not clear.
 
hani se
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:

hani se wrote:I know its impossible but I mean how can we get around it?


So you know it is impossible, but you don't believe it's impossible? You're basically saying "I know I can't flap my arms and fly, but how can I flap my arms and fly?"
.



We don't have concept of "friend" in java but it doesn't mean we "can't" and it's "impossible" to implement this concept,
one way for extend a class having only a private constructor is to use nested classes, "is there any other way???"
I want to know this technically, assume that i can't modify the superclass, is there any other way to do this?
 
Marshal
Posts: 5409
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be worth explaining exactly why you want to know this? It will help give us some context and maybe help you with your actual problem.

There is no way through good solid quality Java programming you can achieve this. Even if you were able to achieve it through some horrible hack, it would not be the right thing to do. If you were working on my team and presented some horrible hack for no good reason, I'd want you off my team immediately.

My advice: Forget about it and focus your energy on more worthwhile problems, like learning how to write really good Unit Tests.
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hani se wrote:
We don't have concept of "friend" in java but it doesn't mean we "can't" and it's "impossible" to implement this concept,
one way for extend a class having only a private constructor is to use nested classes, "is there any other way???"



I kinda think that this argument is moot. Using nested classes doesn't not violate the private concept -- it is perfectly within the usage of private access. Private means private to the top level class, and inner classes are part of the top level class.... in other words, it is not like you found a way around the concept, and asking "is there any other way???".

And no, Java may not have a concept of "friend" function or classes, but it does have package access, which C++ doesn't have a concept of.

Henry
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hani se wrote:We don't have concept of "friend" in java but it doesn't mean we "can't" and it's "impossible" to implement this concept,


No, it doesn't. If you really want to, you can modify a Java String; but that doesn't mean you should, And don't expect anything you implement in this case to be supported by the language because, as you've already been told, IT DOESN'T ALLOW IT.

Now if you simply want to "crack" Java, or find an "end-around" to this particular issue, I think you should first answer the question you've been asked more than once: Why?

However, if you're really interested in knowing why Java has two methods for making a class "final", Henry has already provided a clue: A nested class has access to its outer class's private constructors, which means that a class you write, while publicly "final", can supply private subclasses via its methods - including factories.

And that is VERY useful.

Winston
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic