aspose file tools*
The moose likes Beginning Java and the fly likes Calling super class method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling super class method" Watch "Calling super class method" New topic
Author

Calling super class method

shahabas shabeer
Ranch Hand

Joined: Feb 23, 2004
Posts: 49
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


The greatest pleasure in life is doing what peoples say you can not do.
Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
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.


SCJP 1.4<br />SCWCD 1.3
shahabas shabeer
Ranch Hand

Joined: Feb 23, 2004
Posts: 49
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

Joined: Oct 13, 2002
Posts: 208
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
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

Joined: Oct 04, 2004
Posts: 218
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling super class method