Skip to main content
Post Closed as "exact duplicate" by Jon Skeet, Andrew Hare, Michael Myers, matt b, Tom Hawtin - tackline
Clarifying that both ancestors are not under my control
Source Link
Steve Reed
  • 2.5k
  • 2
  • 21
  • 21

Part of me thinks that this shouldn't be possible (even if it is), but I'll ask anyway.

Given the following class hierarchy (Grandparent and Parent are from a 3rd party and thus, not under my control), how would I override myMethod() in Child such that it bypasses the overridden implementation in Parent and invokes the one in Grandparent?

class Grandparent { public void myMethod() { // do stuff } } class Parent extends Grandparent { @Override public void myMethod() { super.myMethod(); // do something else } } class Child extends Parent { @Override public void myMethod() { // ??? I want to *only* do what Grandparent did here } } 

Pretend that the Parent class provides a lot of other helpful behavior and is a crucial element of Child's hierarchy (in other words, I'm not looking for "make Child a subclass of Grandparent".

Part of me thinks that this shouldn't be possible (even if it is), but I'll ask anyway.

Given the following class hierarchy, how would I override myMethod() in Child such that it bypasses the overridden implementation in Parent and invokes the one in Grandparent?

class Grandparent { public void myMethod() { // do stuff } } class Parent extends Grandparent { @Override public void myMethod() { super.myMethod(); // do something else } } class Child extends Parent { @Override public void myMethod() { // ??? I want to *only* do what Grandparent did here } } 

Pretend that the Parent class provides a lot of other helpful behavior and is a crucial element of Child's hierarchy (in other words, I'm not looking for "make Child a subclass of Grandparent".

Part of me thinks that this shouldn't be possible (even if it is), but I'll ask anyway.

Given the following class hierarchy (Grandparent and Parent are from a 3rd party and thus, not under my control), how would I override myMethod() in Child such that it bypasses the overridden implementation in Parent and invokes the one in Grandparent?

class Grandparent { public void myMethod() { // do stuff } } class Parent extends Grandparent { @Override public void myMethod() { super.myMethod(); // do something else } } class Child extends Parent { @Override public void myMethod() { // ??? I want to *only* do what Grandparent did here } } 

Pretend that the Parent class provides a lot of other helpful behavior and is a crucial element of Child's hierarchy (in other words, I'm not looking for "make Child a subclass of Grandparent".

Source Link
Steve Reed
  • 2.5k
  • 2
  • 21
  • 21

How to override method to invoke superclass' superclass method?

Part of me thinks that this shouldn't be possible (even if it is), but I'll ask anyway.

Given the following class hierarchy, how would I override myMethod() in Child such that it bypasses the overridden implementation in Parent and invokes the one in Grandparent?

class Grandparent { public void myMethod() { // do stuff } } class Parent extends Grandparent { @Override public void myMethod() { super.myMethod(); // do something else } } class Child extends Parent { @Override public void myMethod() { // ??? I want to *only* do what Grandparent did here } } 

Pretend that the Parent class provides a lot of other helpful behavior and is a crucial element of Child's hierarchy (in other words, I'm not looking for "make Child a subclass of Grandparent".