HIU
HIU

Reputation: 19

Java Cast class object to superclass

So I got a method to get all classes in a package, and I have a way to testing if a class is extended to a speciel class

   for (Class c : classes) {
            System.out.println("Class: " + c);
            if (c.getSuperclass().equals(SpecielClass.class)) {
                System.out.println(c + " is SpecielClass");
            }
        }

but now, how do I get the "SpecielClass" methods from the c. If this is even possible?

Upvotes: 0

Views: 69

Answers (1)

meriton
meriton

Reputation: 70564

You can get the SpecielClass methods from the SpecielClass.class object.

(since every instance of c is a SpecielClass, you can invoke these methods on an instance of c).

It also worth noting that c.getMethods() will include methods inherited from a super class (regardless of whether they are overridden in the subclass), while c.getDeclaredMethods() will only get methods declared in class c, but not those inherited from super types.

Upvotes: 1

Related Questions