• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

invoking super constructors.

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, thanks Rob.
...i can work with this.

lovely solution!
 
Get off me! Here, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic