mardi 25 avril 2017

Class.getDeclaredMethods() of reflection is returning unwanted values

I have a class A which is a abstract class, class B is concrete and extends A.

Calling B.class.getDeclaredMethods() returns class A's method signatures in addition to class B's but JAVA documentation on getDeclaredMethods() says

"This includes public, protected, default (package) access, and private methods, but excludes inherited methods."

So from above docs i was expecting method foo() which is inherited from abstract parent class should not be returned from getDeclaredMethods() call, but i am getting method foo() which is inherited from abstract parent class is returned from getDeclaredMethods() call.

import java.lang.reflect.*;

public class B extends A {
    public static void main(String[] args) throws Exception {
        Method[] methods = B.class.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println(methods[i]);
        }
    }
}


abstract class A {
    public void foo() {
    }
}

Can some one explain me this behavior.





Aucun commentaire:

Enregistrer un commentaire