Why Object Class is the Super Class for All Classes in Java



The java.lang.Object class is the root or superclass of the class hierarchy, it belongs to the java.lang package. All predefined classes and user-defined classes are direct or indirect subclasses of the Object class.

Why is Object Class a Superclass?

To understand why the Object class is the superclass of every class in Java, we need to understand why a superclass is needed in the first place:

  • To share common properties.
  • To accept and return any type of object.

Sharing Common Properties using Object Class

There are 11 common properties that every Java class must implement. But it would be difficult for developers to repeat the same boilerplate code in each class. The Object class implements all these 11 properties through 11 methods, making Java programming a little simpler,

All these methods have a generic logic common to all the subclasses, if this logic does not satisfy specific requirements of the subclass, then the subclass can override it.

Example

The toString() method is of the Object class. By default, it returns a string representation of the object's memory address. In the following Java program, we are overriding it to print the name property of the class.

class JavaDev {
   String name;
   // constructor
   JavaDev(String name) {
      this.name = name;
   }
   // overriding toString method of Object class
   @Override
   public String toString() {
      return "Developer Name: {'" + name + "'}";
   }
}
public class Team {
   public static void main(String[] args) {
      JavaDev developer = new JavaDev("Shriansh");
      System.out.println(developer.toString()); 
   }
}

When you run the above code, it will print the following result:

Developer Name: {'Shriansh'}

Accepting and returning any type of Object

A single method can receive and send any type of class object as an argument or return type. It is possible due to a superclass, which is the Object class.

Example

Let's see an example where a method will accept an object as an argument and return it:

class ObjectCls {
   // object type argument
   public Object returnObj(Object obj) {
      System.out.println(obj.toString());
      // return object
      return obj; 
   }
}
public class Main {
   public static void main(String[] args) {
      ObjectCls obj1 = new ObjectCls();
      String str = "This is String Object";
      Integer num = 42;
      obj1.returnObj(str);
      obj1.returnObj(num);
   }
}

When you run the code, it will return and print the passed object:

This is String Object
42

Common Functionalities of Every Class Object

A list of properties that are implemented by every class in Java is given below. We need to remember that these properties are represented by methods and are defined inside the Object class.

Updated on: 2025-05-30T17:21:43+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements