• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to call "super.super.methodName()"  RSS feed

 
Daniel Kaczmarek
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piece of code:

class A {
public void go() {
System.out.println("A");
}
}

class B extends A {
public void go() {
System.out.println("B");
}
}

class C extends B {
public void go() {
System.out.println("C");
}

public void call() {
...
}
}

public class Program {
public static void main(String[] args) {
new C().call();
}
}

Task:

fill the call method in C class so that the output was "A".

So the question is how can I call a method from "super.super" class?
Is this possible?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

There is no way you can call the superclass's superclass. Actually if you have

public class C extends B . . .

and

public class B extends A . . .

that implies that a "C" IS-A "B"; if you try to call the method from "A" then your "C" object NO-LONGER-IS-An "A". So you are breaching the conventions of inheritance. If you search, you will find old threads about the same topic; similar questions come up about every two months.

And welcome to JavaRanch

And I won't tell you how you can cheat with a classcast.
 
Daniel Kaczmarek
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you explain what you mean /if you try to call the method from "A" then your "C" object NO-LONGER-IS-An "A". So you are breaching the conventions of inheritance/ ?

If "C" IS-A "B", and "B" IS-A "A" then "C" IS-A "A".

After all I can call super.go() from "C" (prints "B"), super.go() from "B" (prints "A")...
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you explain what you mean

see here. It will help you.
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:if you try to call the method from "A" then your "C" object NO-LONGER-IS-An "A".
And I won't tell you how you can cheat with a classcast.

I believe you meant NO-LONGER-IS-An "B"? Because it's B one would be skipping. And I hope that the strike means "Don't even try to read it" - class casting won't help as the inherited methods are invoked virtually, so anyway the method closest in the class hierarchy would be selected.
 
Daniel Kaczmarek
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class casting won't help as the inherited methods are invoked virtually, so anyway the method closest in the class hierarchy would be selected.


That's right, classcast won't help. ((A)this).go() neither.

Thanks for the replies.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right. It was NO-LONGER-IS-A "B". Sorry.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I tried the cast like that, and got a StackOverflowError, so there must be some recursion going on.
 
Daniel Kaczmarek
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



And I tried the cast like that, and got a StackOverflowError, so there must be some recursion going on.


I don't get any Exception. The code is correct and prints "C", as "this" is an object of "C" class and classcasting won't change anything.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!