Tip: Understanding Usage of the 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:

[[email protected]]# java NewClassClass of myClass is MyClassSuperclass of myClass is SuperClassClass of superClass is SuperClassSuperclass of myClass is java.lang.Object*/

?

Visit the DevX Tip Bank

?

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

More From DevX