View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

All Classes in Java Are Inherited from Which Class?

Updated on 24/04/20257,194 Views

In Java, everything revolves around classes and objects. As a beginner, one of the first concepts you'll encounter is inheritance, which allows one class to acquire the properties of another. But did you know that every class in Java, even the ones you create, inherits from a single universal class?

In this blog, we’ll explore which class is inherited by all Java classes, understand why it matters, and look at some simple examples and terminology to clarify the concept.

Learning Java is just the beginning. Take the next step with upGrad’s Software Engineering program designed for future-ready developers.

Which Class is Inherited by All the Java Classes?

In Java programming, the answer to the question of which class is inherited by all other classes lies in the fundamental class hierarchy.  All classes in Java inherit from java.lang.Object class, either directly or indirectly.

The "Object" class is the root class of the Java class hierarchy and serves as the ultimate superclass for all other classes. This means that every class in Java is implicitly a subclass of the "Object" class

The "Object" class provides a set of methods and functionalities that are common to all Java objects. These methods include "equals()", "hashCode()", "toString()", and more. From the "Object" class, all other classes inherit these fundamental methods, allowing for consistent behavior and interoperability among objects.

Why Do All Classes Inherit from Object Class?

This design provides a common structure and behavior across all classes. Here’s why it’s important:

  • Ensures consistency in object handling.
  • Enables polymorphism—you can store different types of objects in an Object[] array.
  • Allows all classes to use basic utility methods like toString(), equals(), and hashCode().

Already know Java? Expand your skill set with data science and AI through IIIT-B’s Executive Diploma program on upGrad.

Exploring Object Class with Examples

Example 1: Creating a Custom Class

class Animal {
    void speak() {
        System.out.println("Animal speaks");
    }
}

Output:

No output yet – just a class definition.

Explanation:

Even though we didn’t write extends Object, the Animal class still inherits from Object automatically.

Example 2: Using Methods from Object Class

class Animal {
    String name = "Tiger";
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Animal();
        System.out.println(a.toString());
        System.out.println(a.hashCode());
        System.out.println(a.getClass());
    }
}

Output  (will vary):

Animal@6d06d69c
12345678
class Animal

Explanation:

  • toString() gives the object’s class name and hashcode.
  • hashCode() returns a unique integer for the object.
  • getClass() returns the runtime class of the object.

Ready to transition from Java developer to data scientist? Kickstart your journey with the Executive Diploma in Data Science & AI by IIIT-B.

Key Terminologies in Java Inheritance

  • Superclass

A superclass is a class that is extended by other classes. In Java, the Object class is the superclass of all other classes.

  • Subclass

A subclass is a class that extends another class.

Example:

class Dog extends Animal {}

  • IS-A Relationship

Describes inheritance: a subclass is a type of its superclass. E.g., A Dog is an Animal.

  • Multilevel Inheritance

In mutiple inheritance, a class inherits from a class which itself inherits from another.

class A {}
class B extends A {}
class C extends B {}
Here, C indirectly inherits from A.

What Is the Base Class of All Classes in Java?

The base (or root) class of all Java classes is java.lang.Object. This class is automatically included in every Java project and does not require importing.

It provides key methods that every Java object can use without additional code.

The Superclass of Every Class: Deep Dive

Let’s explore some common methods of the Object class with examples:

1. toString()

Returns a string representation of the object.
class Sample {}

public class Main {
    public static void main(String[] args) {
        Sample s = new Sample();
        System.out.println(s.toString());
    }
}

Output:

Sample@1b6d3586

Explanation:Shows class name and memory address (hashcode in hex).

2. equals(Object obj)

Checks whether two objects are equal.
public class Main {
    public static void main(String[] args) {
        String a = new String("hello");
        String b = new String("hello");
        System.out.println(a.equals(b));
    }
}

Output:

true

Explanation:The equals() method in the String class is overridden to compare the actual content of the strings. Since both a and b contain "hello", the result is true, even though they are different objects in memory.

3. hashCode()

Returns a unique hash value for the object.
class Sample {}

public class Main {
    public static void main(String[] args) {
        Sample s = new Sample();
        System.out.println(s.hashCode());
    }
}

Output:

12345678 (varies each time)

Explanation:The hashCode() method returns an integer hash value of the object, which is used in hashing-based collections like HashMap. The actual number may vary each time the program is run because it’s based on the object’s memory address or internal hashing logic.

4. getClass()

Returns the runtime class of the object.
class Sample {}

public class Main {
    public static void main(String[] args) {
        Sample s = new Sample();
        System.out.println(s.getClass());
    }
}

Output:

class Sample

Explanation:

The getClass() method (inherited from the Object class) returns the runtime class of the object. In this case, the output shows that the object s is an instance of the Sample class.

Parent Class in Java: How It Works Internally

When you write a class like this:

class MyData {}

The compiler internally treats it as:

class MyData extends Object {}

You can verify this by checking decompiled bytecode. This is how Java ensures every class has a base set of methods.

Conclusion

To sum it up:

  • In Java, every class inherits from the Object class, making it the universal superclass.
  • This inheritance gives all classes a common set of useful methods.
  • Understanding Object helps you debug better, write reusable code, and appreciate Java’s design principles.

Whether you're just starting or brushing up your basics, this concept is fundamental to mastering Java's object-oriented system.

Top FAQs on All Classes in Java Are Inherited from Which Class?

1. Which class is the parent of all classes in Java?

All classes in Java implicitly inherit from the Object class, either directly or indirectly. It is the root class of the Java class hierarchy and provides common methods like toString(), equals(), hashCode(), and getClass().

2. Do user-defined classes automatically inherit from the Object class?

Yes, every class created in Java without explicitly extending another class will automatically inherit from the Object class. This ensures that all Java classes have access to the basic utility methods provided by Object.

3. Is inheritance from the Object class optional in Java?

No, inheritance from the Object class is not optional. It’s enforced by the Java language specification to maintain consistency and ensure that all objects share a common base type.

4. What methods are inherited from the Object class?

Common methods inherited from the Object class include toString(), equals(), hashCode(), getClass(), clone(), wait(), notify(), and finalize(). These provide basic object behavior across all Java classes.

5. Can a class extend a class other than Object?

Yes, a class can extend another user-defined or library class. However, all such classes will still eventually inherit from Object either directly or indirectly through the extended class.

6. Is Object class part of java.lang package?

Yes, the Object class is part of the java.lang package, which is automatically imported in every Java program. You don’t need to manually import it to use its features.

7. What is the significance of Object class in Java?

The Object class provides a base for Java’s inheritance hierarchy. It allows all classes to be treated uniformly and enables polymorphism, reflection, serialization, and interoperability with collection classes like ArrayList and HashMap.

8. Can the Object class be extended directly?

Yes, you can explicitly extend the Object class, but it’s usually unnecessary since every class already inherits from it by default. Explicitly writing extends Object doesn’t change the behavior.

9. Are interfaces inherited from the Object class?

Interfaces themselves do not extend the Object class. However, when a class implements an interface, it still inherits the Object class, so the interface's implementing class can use Object's methods.

10. Is Object class abstract in Java?

No, the Object class is not abstract. You can create instances of it directly using new Object(), although it’s uncommon to use it in this way without subclassing.

11. Why does every Java class inherit from the Object class?

Inheriting from the Object class ensures that every class has a standard set of methods and can interact with core Java features like collections, serialization, and runtime type identification uniformly.

image

Take the Free Quiz on Java

Answer quick questions and assess your Java knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Explore Our Free Software Tutorials

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.