Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.lang.reflect.Method;
/**
* @author Nick Volynkin nick.volynkin@gmail.com
*/
public class SuperDuperMethodTest {
public static void main(String... args) {
C c = new C();
c.method2();
}
}
class A {
public void method() {
System.out.println("A");
}
}
class B extends A {
@Override
public void method() {
System.out.println("B");
}
}
class C extends B {
@Override
public void method() {
System.out.println("C");
}
public void method2() {
try {
Class<?> superDuperClass = this.getClass().getSuperclass().getSuperclass();
System.out.println(superDuperClass);
Method method = superDuperClass.getDeclaredMethod("method");
method.invoke(new A()); //A
method.invoke(new B()); //B
method.invoke(new C()); //C
method.invoke(this); //C
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.