• 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

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79417
377
  • 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 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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")...
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Could you explain what you mean


see here. It will help you.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79417
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right. It was NO-LONGER-IS-A "B". Sorry.
 
Campbell Ritchie
Marshal
Posts: 79417
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic