Tuesday, 13 October 2015

Obtain Superclass and implemented Interface

Obtain Superclass and implemented Interface using class Object methods

Using getSuperclass() method of Class object, we can access the superclass of the class.

package reflect;
class Super {
}
class Child extends Super {
}
public class ClassObject {
     public static voidmain(String[] args) {
           Class superCls = Child.class.getSuperclass();
           System.out.println(superCls);
     }
}
Output: class reflect.Super


Implemented Interfaces
A class can implement many interfaces. Therefore an array of Class is returned. Interfaces are also represented by Class objects in Java Reflection.

Class[] interfaces1 = MyClass.getInterfaces();

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...