| Author |
Why subclass doesn't inherit constructor's superclass ?
|
Julien Va
Greenhorn
Joined: Sep 24, 2010
Posts: 4
|
|
Hi everyone,
I know that a subclass can't inherit from the constructor's superclass.
If the superclass A has 5 overloaded constructors (with different parameters), then sub-class B extending A will not have these constructors available from the outside.
if I can do: new A(param1, param2),
I cannot do: new B(param1, param2)
unless I explicitely redefine the constructors of A, in the B class (which is boring isn't it?).
And I know that I can use the super type to access at the attributes of the superclass
So, my question is : "Why the conceptors of Java prevented the constructors to be inherited?"
I need those information to make a presentation in front of my classroom.
Thank you very much for your help.
|
When I try to use google, I find answers on forums where we tell me to look on Google. Think about it...
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Julien Va wrote:
unless I explicitely redefine the constructors of A, in the B class (which is boring isn't it?).
You can't do this! Where do you get this?
Julien Va wrote:
And I know that I can use the super type to access at the attributes of the superclass
Not the super type, but, super keyword!
Julien Va wrote:
So, my question is : "Why the conceptors of Java prevented the constructors to be inherited?"
What is the logic, if you allow to construct an object of another class in you class?
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
In java, only members can be inherited. Constructors are not a member of a class.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Abimaran Kugathasan wrote:
Julien Va wrote:
unless I explicitely redefine the constructors of A, in the B class (which is boring isn't it?).
You can't do this! Where do you get this?
I think what Julien meant was that he could define 5 overloaded constructors in B which matched the signatures of the 5 constructors in A
i.e.
|
Joanne
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Joanne Neal wrote:
I think what Julien meant was that he could define 5 overloaded constructors in B which matched the signatures of the 5 constructors in A
i.e.
In some case[probably most of the case ], this will cause more trouble than it worth . this might be one of the reason to not allowing the inherited constructor in subclass.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Welcome to JavaRanch Julien
|
 |
Julien Va
Greenhorn
Joined: Sep 24, 2010
Posts: 4
|
|
Seetharaman Venkatasamy wrote:Welcome to JavaRanch Julien 
Thanks
But you say also :
Seetharaman Venkatasamy wrote:this will cause more trouble than it worth
Why ? Witch trouble ?
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Julien Va wrote:
But you say also :
Seetharaman Venkatasamy wrote:this will cause more trouble than it worth
Why ? Witch trouble ?
For an example: suppose you dont want B to be subclass or you want to expose only default constructor to a class/subclass to avoid the confusion of many overloaded constructor.
|
 |
Julien Va
Greenhorn
Joined: Sep 24, 2010
Posts: 4
|
|
Seetharaman Venkatasamy wrote:
For an example: suppose you dont want B to be subclass
Do you have an exemple please ?
Seetharaman Venkatasamy wrote:or you want to expose only default constructor to a class/subclass to avoid the confusion of many overloaded constructor.
I think that could be one answer. I understant what you meen. I don't think about that way until now... tanks !
|
 |
Julien Va
Greenhorn
Joined: Sep 24, 2010
Posts: 4
|
|
Up
So, Seetharaman said that it's a solution to avoid the confusion of many overloaded constructor. I agree with him. But, do you have other proposition ?
Thank you
|
 |
 |
|
|
subject: Why subclass doesn't inherit constructor's superclass ?
|
|
|