wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes invoking super constructors. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "invoking super constructors." Watch "invoking super constructors." New topic
Author

invoking super constructors.

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
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

Joined: Nov 28, 2007
Posts: 215

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

Joined: Feb 20, 2006
Posts: 132
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

Joined: Dec 05, 2005
Posts: 134
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
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
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

Joined: Oct 27, 2005
Posts: 19676
    
  18

Don't just copy code from ClassA to ClassC - instead, let ClassC have an instance variable of type ClassA.

You'll get the following:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
oh, thanks Rob.
...i can work with this.

lovely solution!
 
Consider Paul's rocket mass heater.
 
subject: invoking super constructors.