This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 23586
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: 21452
95
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!