Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

invoking super constructors.  RSS feed

 
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.
 
  • 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.
 
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?
 
author
Sheriff
Posts: 23510
138
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.....
 
Sheriff
Posts: 21330
87
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!
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!