Welcome To The World Of Java

This site will teach you the basics of Java. It is not necessary to have any prior programming experience.

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

Somdeb Burman | 11:01:00 PM | 0 comments

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

Java Resources are available for Download from below

Category: , ,

0 comments