tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Java > Reflection > Get Parent Class

Get Parent Class 

The following example shows how to access the parent class of a given class. The super class of java.lang.Object is null. Class provides the following APIs,

public Class<? super T> getSuperclass()
 - Returns parent class of this class. 


File Name  :  
com/bethecoder/tutorials/reflection/GetParentClass.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.reflection;

import java.util.Stack;

public class GetParentClass {

  /**
   @param args
   */
  public static void main(String[] args) {
    printParents(String.class);
    printParents(Integer.class);
    printParents(Stack.class);
  }
  
  private static void printParents(Class<?> clazz) {
    
    while (clazz != null) {
      System.out.print(clazz.getName());
      
      if (clazz.getSuperclass() != null) {
        System.out.print(" > ");
      }
      clazz = clazz.getSuperclass();
    }
    System.out.println();
  }

}
   

It gives the following output,
java.lang.String > java.lang.Object
java.lang.Integer > java.lang.Number > java.lang.Object
java.util.Stack > java.util.Vector > java.util.AbstractList > 
	java.util.AbstractCollection > java.lang.Object


                    Print

   



Books Recommendation

 
 
bl  br