Calling super super class method

Let's say I have three classes A, B and C.

  • B extends A
  • C extends B

All have a public void foo() method defined.

Now from C's foo() method I want to invoke A's foo() method (NOT its parent B's method but the super super class A's method).

I tried super.super.foo();, but it's invalid syntax. How can I achieve this?


You can't even use reflection. Something like

Class superSuperClass = this.getClass().getSuperclass().getSuperclass();
superSuperClass.getMethod("foo").invoke(this);

would lead to an InvocationTargetException, because even if you call the foo-Method on the superSuperClass, it will still use C.foo() when you specify "this" in invoke. This is a consequence from the fact that all Java methods are virtual methods.

It seems you need help from the B class (e.g. by defining a superFoo(){ super.foo(); } method).

That said, it looks like a design problem if you try something like this, so it would be helpful to give us some background: Why you need to do this?


You can't - because it would break encapsulation.

You're able to call your superclass's method because it's assumed that you know what breaks encapsulation in your own class, and avoid that... but you don't know what rules your superclass is enforcing - so you can't just bypass an implementation there.


You can't do it in a simple manner.

This is what I think you can do:

Have a bool in your class B. Now you must call B's foo from C like [super foo] but before doing this set the bool to true. Now in B's foo check if the bool is true then do not execute any steps in that and just call A's foo.

Hope this helps.