2

Is there any way to skip java inheritance hierarchy and call method of super super class?

I have 3 classes in java

X
Y extends X
Z extends Y.

Now is there any way to access any method of X directly skipping Y ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128

4 Answers4

3

Here Why is super.super.method(); not allowed in Java? They claim you cannot and there's a good discussion of why.

edit I'm assuming you have a situation like foo() is defined in X and Y and you want to call it from X in Z

Community
  • 1
  • 1
MadDogMcNamara
  • 312
  • 1
  • 9
2

Now is there any way to access any method of X directly skipping Y?

Practically no, theoritically yes. Following your example:

X
Y extends X
Z extends Y.

When you call a method of Z, it will call super() which will call method of Y if any and it will call super() too, which will call method of X in the end. I said yes, when method of Y does nothing different from X's, then it is as if it has no effect. But in practice, JVM follows the inheritance hierarchy to complete the calls, bear in mind that.

Juvanis
  • 25,802
  • 5
  • 69
  • 87
1

I guess you could use reflection API, but it's kind of ugly IMO.

Qben
  • 2,617
  • 2
  • 24
  • 36
0

Yes, you can get access to all methods of X except private methods

Gleb Belyaev
  • 1,009
  • 1
  • 14
  • 23