lundi 19 juin 2017

reflection - getMethods()/getDeclaredMethods() (Duplicate Methods!)

Consider the following simple Java code snippet:

package p;   

class Sup {
    public Sup f() {return null;}
}

class Sub extends Sup {
    @Override public Sub f() {return null;}
}

I used Java Reflection to list all the (declared) methods of the class Sub as follows.

for(Method m : Sub.class.getDeclaredMethods())
            System.out.println(m);

But I received an output like the following:

public p.Sub p.Sub.f()
public p.Sup p.Sub.f()

The first line is pretty reasonable, but the second one confuses me. Please explain why do I get the method public p.Sup p.Sub.f()? How does JVM distinguishes between the two methods at runtime during virtual method call resolution?

Thank you.





Aucun commentaire:

Enregistrer un commentaire