What is the root class in Java?

Java 8Object Oriented ProgrammingProgramming

The Object class of the java.lang package is the root class in Java i.e. It is the super class of every user-defined/predefined class n Java. All objects, including arrays, implement the methods of this class.

The reason for this is to have common functionalities such as synchronization, garbage collection, collection support, object cloning for all objects in Java.

The class named Class of java.lang package provides a method named getSuperclass() this method returns the Class representing the superclass of the current Class.

So, Create a sample concrete class and try to get the name of its super class using this method.

Example

Live Demo

public class Sample {
   public static void main(String args[]) {
      Sample obj = new Sample ();      
      Class cls = obj.getClass().getSuperclass();
      System.out.println(cls.getName());     
   }
}

Output

Since the Object class is the super class of all classes it displays the name of the object class as shown below.

java.lang.Object
raja
Published on 05-Jan-2018 07:14:36
Advertisements