Inheritance
The mechanisms of deriving new class from an old class is called inheritance. The old class is called the base class or super class or parent class and the new class is called derived class or sub class or child class.
Inheritance is a fundamental concept of object-oriented programming that enables one class to inherit the properties and methods of another class. In Java, inheritance is implemented through the “extends” keyword, which allows a subclass to inherit the properties and methods of a superclass. In this blog, we will cover all the topics related to inheritance in Java.

Syntax of Inheritance : we can define a class that extends an existing class using the following syntax:
class SubClass extends SuperClass {
// fields and methods
}
In the above code, SubClass
is the name of the new class we are defining, and SuperClass
is the name of the class that we want to extend. The extends
keyword is used to specify the superclass.
Types of Inheritance in Java There are four types of inheritance in Java:
Single Inheritance: A subclass can inherit only one superclass in single inheritance.
Multilevel Inheritance: A subclass can inherit a superclass, and the same subclass can act as a superclass for another subclass.
Hierarchical Inheritance: Multiple subclasses can inherit a single superclass.
Multiple Inheritance: A subclass can inherit multiple superclasses. Java doesn’t support multiple inheritances, but it can be achieved through interfaces.
1. Single Inheritance:
Single Inheritance is the most common type of inheritance in Java. In Single Inheritance, a subclass inherits properties and methods from a single superclass. In other words, a class extends only one class.
class SuperClass {
// superclass methods and fields
}
class SubClass extends SuperClass {
// subclass methods and fields
}
2. Multilevel Inheritance:
Multilevel Inheritance is a type of inheritance in which a subclass extends a superclass and the same subclass acts as a superclass for another subclass. In this type of inheritance, a class can inherit properties and methods from more than one class.
class SuperClass {
// superclass methods and fields
}
class SubClass extends SuperClass {
// subclass methods and fields
}
class SubSubClass extends SubClass {
// subclass methods and fields
}
3. Hierarchical Inheritance:
Hierarchical Inheritance is a type of inheritance in which multiple subclasses inherit properties and methods from a single superclass. In this type of inheritance, a class can act as a superclass for more than one subclass.
class SuperClass {
// superclass methods and fields
}
class SubClass1 extends SuperClass {
// subclass1 methods and fields
}
class SubClass2 extends SuperClass {
// subclass2 methods and fields
}
4. Multiple Inheritance:
Multiple Inheritance is a type of inheritance in which a subclass can inherit properties and methods from multiple superclasses. Java does not support multiple inheritances for classes, but it can be achieved using interfaces.
interface Interface1 {
// interface1 methods and fields
}
interface Interface2 {
// interface2 methods and fields
}
class MyClass implements Interface1, Interface2 {
// class methods and fields
}
In the above code, MyClass
implements both Interface1
and Interface2
, which means it inherits properties and methods from both interfaces.
To summarize, inheritance is a powerful concept in object-oriented programming that allows classes to inherit properties and methods from other classes. In Java, there are four types of inheritance: Single Inheritance, Multilevel Inheritance, Hierarchical Inheritance, and Multiple Inheritance. Understanding these types of inheritance is crucial for building complex and scalable software applications in Java.