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*/
See also  Java Developer Jobs: Your Guide to Landing the Perfect Position in 2024
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist