• 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

Calling super class method

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

Assume that class A extends class B, which extends class C. Also all the three classes implement the method test().

I know that A can invoke method 'test()' in B using super.test().

How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C).

Thanks
Shahabas E Shabeer
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since class A can call method in class B using super.test(), Class B can invoke method from class C by doing super.test(). That way test() in class C will be called from A.
 
shahabas shabeer
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply,
But is there a way to call test directly from class A ?
Something like super.super.test() (I know this won't work).
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only other way I know is by creating an instance of class C from class A. To my best of knowledge super.super.test() will not work.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, there is no way to call a method in a grandparent class directly. You can only call methods in the parent class. Note that if you create an instance of the grandparent class (class C in this case), then calling a method on it will have no direct effect on the current instance of class A. This behavior is very different from what would happen if you call supper.test() to call the test() method in B which in turn also calls super.test() to call the test() method in C.

Anyway, I hope this answers your question.

Keep Coding!

Layne
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if I'm butting in on someone elses question but I understood that:

If you had class A (this would be the super class)

then if you had class B (this would be the sub class)

you couldn't have more than two classes with inheritance? i.e. you would therefore have to use an interface? Have I gone wrong with my understanding of inheritance?
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please ignore the previous comment (I have just made a right fool of myself)!

To clarify what I was getting confused with after reading this question...

Class A is the parent class (therefore a super class of all the following classes).

Class B is the sub class of Class A
(Class B can not have more than one super classes! And inherits everything from Class A)

Class C is the sub class of Class B
(Class C inherits from Class B which inherits from Class A, so B gets everything)

That'll teach me NEVER to but in again.
I am Sorry!
 
Squanch that. And squanch 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