All Classes in Java are Inherited From Which Class?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Which Class is Inherited by all the Java Classes?

Object class is inherited by all the classes in Java. It is the parent class of every class in Java either directly or indirectly. Since it is the parent class of all classes, therefore, all its methods are available to all other classes in Java. Therefore, Object class behaves as a root of inheritance hierarchy in Java.

Since Java supports hierarchical inheritance, all the classes are directly or indirectly inherited from the Object class of Java. If a class does not extend any class then it will be directly linked to the Object class, that is, it becomes a direct child of the Object class. However, if a class is extending any other class then it forms an indirect link to the Object class. This is because multiple inheritance is not supported in Java. Hence, a class can either inherit the Object class or any other class but not both of them.

The Object class has many methods such as hashCode(), getClass(), toString() which are used by all the classes. Therefore, we do not need to again write these methods in our code, we can simply extend the Object class and use them in our class.

Examples

Inheritance is a mechanism in which a class acquires or inherits the properties of another class. Let us see some examples of inheritance in Java.

Terminologies Used

  • Parent class: The class whose properties are inherited is called the parent class, superclass or base class.
  • Child class or subclass or derived class: The class that inherits the properties of another class is called the child class, subclass or derived class.

There are various types of inheritance supported in Java as mentioned below:

  • Single Inheritance - In single level inheritance, a class inherits the properties of only one base class.
  • Multilevel Inheritance - In this type of inheritance, one subclass is derived from is superclass which is also derived from another superclass. Therefore, there are multiple parent classes of a single class.
  • Hierarchical Inheritance - If multiple sub classes are derived from a single base class then it is known as the hierarchical inheritance.
  • Hybrid Inheritance - Hybrid means more than one. Therefore, hybrid inheritance is a collection of two or more types of inheritance.

IS-A Relationship

These two classes (superclass and subclass) involved in inheritance have a parent-child relationship which is also known IS-A relationship.

Let us understand this IS-A relationship concept with an example. Suppose you have a class Car and a class Audi. Since Audi is a Car, this relationship is true, therefore, the Car class can be inherited by the Audi class.

Java Inheritance

Single Level Inheritance

Single Level Inheritance refers to the scenario when a class inherits the properties from only one class. In single-level inheritance, there is only one level of inheritance which means the superclass does not inherit again from any other class.

There may be multiple subclasses inheriting from a single superclass but the superclass does not inherit from any other class.

Below is the code which shows the usage of single level inheritance.

Output:

Explanation:

In the above example, we have two classes - Parent and Child where the Child class inherits the Parent class. Here, the Parent class does not again from any other class, hence this is an example of Single Level Inheritance.

Multilevel Inheritance

Now let us understand, multilevel inheritance in Java. In multilevel inheritance, a derived class will be inherited from a base class and this class will also act as the base class for another derived class and so on.

Let us understand it with an example.

Output:

Explanation:

In the above example, Child1 class inherits from the Parent class, Child2 class inherits from Child1 class, whereas Child3 inherits from Child2. Therefore, all these classes make a hierarchy of classes.

Learn More

Various types of inheritance are explained in the article Inheritance in Java. You can refer to it to understand more about inheritance in Java.

Conclusion

  • All the classes in Java are inherited from the Object class. Object class is the parent class in Java.
  • All classes in Java directly or indirectly inherit the Object class.
  • Inheritance is an object-oriented concept in which one class uses the properties and behavior of another class.
  • There are various types of inheritance supported in Java such as:
    • Single Level inheritance
    • Multi-level inheritance
    • Hierarchical inheritance
    • Hybrid inheritance
  • Inheritance provides us with the feature of reusability.