Skip to content

Instantly share code, notes, and snippets.

@NickVolynkin
Created November 10, 2014 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickVolynkin/2c467c5e7a1d78a03058 to your computer and use it in GitHub Desktop.
Save NickVolynkin/2c467c5e7a1d78a03058 to your computer and use it in GitHub Desktop.
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