Get Super classes of a Class Java Reflection API

It is advised to read the notes on Java Reflection API before attempting the programs.

Example to get super classes of a class with Java Reflection API
import java.awt.Button;
import java.lang.reflect.Modifier;

class Test {  }
class Hello extends Test {  }
public class Demo extends Hello
  public static void main(String [] args) 
    Demo d1 = new Demo();
    Class c = d1.getClass();       

// to retrieve the superclasses of a class

    Class sclass = c.getSuperclass();
    while(sclass != null)  
      System.out.println( sclass.getName());
      sclass = sclass.getSuperclass();
    }           // prints Hello, Test, java.lang.Object

Leave a Comment

Your email address will not be published.