Sunday, October 28, 2012

Retrieve SuperClass information using Java Reflection API

Using Java Reflection APIs you can retrieve superClass name, it's simple name,packagename, implemented interfaces and many more.

Key points to retrieve SuperClass Information :
  1. Create Object of class which has super class
  2. Get SuperClass using getSuperClass()
  3. Get SuperClass name using getName()
  4. Get SuperClass simple name using getSimpleName().
  5. Get SuperClass PackageName using getPackage()
  6. Get interfaces using getInterfaces()

 Java Program to get Super Class Information using Java Reflection APIs:

    List list = new ArrayList();
    
    System.out.println("\nSuper Class Information");
    Class superClass = originalClass.getSuperclass();
    
    System.out.println("ClassName - " + superClass.getName());
    System.out.println("Simple Name - " + superClass.getSimpleName());
    System.out.println("PackageName - " + superClass.getPackage());
    Class[] sinterfaces = superClass.getInterfaces();
    
    System.out.println("Implemented Interfaces - ");
    for (int i = 0; i < sinterfaces.length; i++) {
        System.out.println(sinterfaces[i].getName());
    }

Output :

Super Class Information
ClassName - java.util.AbstractList
Simple Name - AbstractList
PackageName - package java.util, Java Platform API Specification, version 1.7
Implemented Interfaces - 
java.util.List

Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/

No comments:

Post a Comment