Write a java program to find name of the class and its super class name by passing the class name at runtime?



class Ref1 {
 public static void main(String[] args) {
   if (args.length == 0) {
    System.out.println("PLEASE PASS THE CLASS NAME..!");
   } else {
    try {
     Class c = Class.forName(args[0]);
     printSuperclass(c);
    } catch (ClassNotFoundException cnfe) {
     System.out.println(args[0] + " DOES NOT EXISTS...");
    }
   }  // else
  }   // main
 static void printSuperclass(Class c) {
   String s = c.getName();
   Class sc = c.getSuperclass();
   String sn = sc.getName();
   System.out.println(sn + " IS THE SUPER CLASS OF " + s);
  }   // printSuperclass
}     // ref1

Output:

java ref1 java.awt.TextField
java.awt.TextComponent IS THE SUPER CLASS OF java.awt.TextField


No comments:

Post a Comment