• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling super class method

 
shahabas shabeer
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jay Ashar
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Maureen Charlton
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic