devxlogo

Understanding the Usage of getSuperclass Method on a Class Object

Understanding the Usage of getSuperclass Method on a Class Object

class SuperClass{   public static void main(String args)   {      //do Nothing   }}class MyClass extends SuperClass{   public static void main(String args[])   {      //do Nothing   }}public class NewClass {   public static void main(String args[])   {      SuperClass superClass = new SuperClass();      MyClass myClass = new MyClass();            //Initializing the calss to that of MyClass      Class classInstance = myClass.getClass();            System.out.println("Class of myClass is " + classInstance.getName());      classInstance = classInstance.getSuperclass();      System.out.println("Superclass of myClass is " + classInstance.getName());            //Initializing the calss to that of SuperClass      classInstance = superClass.getClass();            System.out.println("Class of superClass is " + classInstance.getName());      classInstance = classInstance.getSuperclass();      System.out.println("Superclass of myClass is " + classInstance.getName());               }   }/*

Expected output:

[root@mypc]# java NewClassClass of myClass is MyClassSuperclass of myClass is SuperClassClass of superClass is SuperClassSuperclass of myClass is java.lang.Object*/

devx-admin

Share the Post: