What is the root class in Java?

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.


Live Demo

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


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