Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invoking super constructors.

 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would like to know the following:

if:

ClassA extends ClassB

and

ClassC extends ClassA


how do i (inside ClassC)....

invoke the constructor of Class B ?

thanks

-W.O.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wolfgang Obi:
i would like to know the following:

if:



how do i (inside ClassC)....

invoke the constructor of Class B ?

thanks

-W.O.


I think your question subject itself has the answer.
Can you write a sample code which you tried with what you got as output.
 
Petrus Pelser
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor of class B will implicitly be called when you create a new object of class C. When you create a new object of class C, the following will happen:

The constructor of class C will call the constructor of class A, which in turn will call the constructor of class B.

Note that a subclass constructor will call the super class' constructor before executing any code in the subclass constructor.

You do not have to call manually, unless the super class constructor contains arguments.
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Petrus Pelser:


You do not have to call manually, unless the super class constructor contains arguments.


well, exactly this is the case here.

i am calling manually because the super class constructor contains arguments.

what do i do in such a case?
 
Henry Wong
author
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do i do in such a case?


Simply put, you can't.

The way you have it, your classC *IS-A* classA which *IS-A* classB. There is a reason why classA removed functionality from classB. If you were able to bypass it, there is no way for the classA portion to be constructed -- hence, your classC can't be a classA anymore.

If you don't care about the functionality of classA, you can always bypass it and inherit from classB directly. Of course, then you have problems when your classC is needed in place of classA objects.

Henry
[ January 29, 2008: Message edited by: Henry Wong ]
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


If you don't care about the functionality of classA, you can always bypass it and inherit from classB directly. Of course, then you have problems when your classC is needed in place of classA objects.

Henry



actually,

it's actually the ClassA functionality i care about...

however: i suppose the way to go will be to inherit from classB direct, and try to reimplement all the methods in class A.....
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't just copy code from ClassA to ClassC - instead, let ClassC have an instance variable of type ClassA.

You'll get the following:
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, thanks Rob.
...i can work with this.

lovely solution!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic