What is the super class of every class in Java?



The Object class is the superclass of every single class in Java. This position implies that every class in Java, even if a built-in class or function like String or a user-defined one, directly or indirectly inherits from Object.

Understanding the Object Class

The Object class provides fundamental methods that every Java object inherits. Even if a class does not explicitly extend another class, it implicitly extends Object. Below is a demostration of the same ?

class Example {  
    //This class implicitly extends to an Object
}

Why Does Every Class Inherit from Object?

The Object class serves as the foundation for the Java class hierarchy. It provides a set of common methods and behaviors that are important for all objects in Java. Every class inherits from an Object. Java ensures that all objects share a common set of functionalities, which promotes consistency and simplifies the language's design.

Let's test it with an example. The java.lang.Class.getSuperclass() returns the Class representing the superclass of the entity (class, interface, primitive type, or void) represented by this Class.

So, Create a sample concrete class, and let's try to get the name of its superclass using this method.

Example

Below is an example of representing the superclass of the entity ?

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

Output

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

java.lang.Object

Conclusion

The Object class is, therefore, a significant segment in the Java inheritance mechanism and guarantees that all Java classes must have a common ancestor. It also provides methods for object comparison, identity, and class metadata but is the bare minimum of those essential methods.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-02-28T18:48:15+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements