---
title: Hierarchical Inheritance in Java with Example - Scaler Topics
description: Get a detailed understanding of the concept of Hierarchical Inheritance in Java with examples and applications only on Scaler Topics.
author: Sufiyan Khan
category: Java
---
:::section{.main}
In Java, [Inheritance](https://www.scaler.com/topics/java/inheritance-in-java/) is a core concept in object-oriented programming that facilitates the creation of new classes based on existing ones, promoting code reuse and extension. It establishes the IS-A relationship, where a child class inherits properties and behaviours from its parent. Through keywords like 'extends', Java supports five types of inheritance: single, multilevel, [multiple](https://www.scaler.com/topics/multiple-inheritance-in-java/), [hybrid](https://www.scaler.com/topics/hybrid-inheritance-in-java/), and hierarchical.
:::
:::section{.main}
## Hierarchical Inheritance in Java

Hierarchical Inheritance in Java is one type of inheritance where multiple child classes inherit the methods and properties of the same parent class. As child classes get access to the methods and properties of the parent class using Hierarchical inheritance, they don't have to create those methods and properties separately; they just need to create the class-specific methods or properties.
### How Does It Work?
We already know that "Hierarchical inheritance" occurs when multiple child classes inherit the methods and properties of the same parent class. In Java, this simply means we have only one superclass and multiple subclasses in hierarchical inheritance.
The idea is simple: To use the methods and properties of the parent class instead of creating them again in the sub-class. **This reduces the code length and increases the code modularity.**
The working of hierarchical inheritance in Java is very straightforward. We need a base class and some subclasses that inherit the base class. Hence, first, we create a `base class` (also called a `Superclass`), then we create some subclasses that inherit that same base class. This is how we can use the code of the base class in subclasses of that base class.
### Why Use Hierarchical Inheritance in Java?
In programming, code repetition is considered a bad practice as it unnecessarily increases the length of the code. A good programmer is expected to deliver clean and reusable code. **You may read** [Clean Code](https://www.scaler.com/topics/java/java-clean-code/) in Java article to learn more about clean code in Java. Inheritance is one of the codes that can be reused to a great extent. Hierarchical inheritance is beneficial in reusing a piece of code. Now, it's time to discuss how hierarchical inheritance works in Java. Here we go.
:::
:::section{.main}
## Hierarchical Inheritance Example
For better understanding, we will start by explaining a basic example of Hierarchical inheritance in Java and then move on to a real-world example.
### Basic Example of Hierarchical Inheritance in Java
**Code:**
```java
// creating the base class(or superclass)
class BaseClass
{
int parentNum = 10;
}
// creating the subclass1 that inherits the base class
class SubClass1 extends BaseClass
{
int childNum1 = 1;
}
// creating the subclass2 that inherits the base class
class SubClass2 extends BaseClass
{
int childNum2 = 2;
}
// creating the subclass3 that inherits the base class
class SubClass3 extends BaseClass
{
int childNum3 = 3;
}
public class Main
{
public static void main(String args[])
{
SubClass1 childObj1 = new SubClass1 ();
SubClass2 childObj2 = new SubClass2 ();
SubClass3 childObj3 = new SubClass3 ();
System.out.println("parentNum * childNum1 = " + childObj1.parentNum * childObj1.childNum1); // 10 * 1 = 10
System.out.println("parentNum * childNum2 = " + childObj2.parentNum * childObj2.childNum2); // 10 * 2 = 20
System.out.println("parentNum * childNum3 = " + childObj3.parentNum * childObj3.childNum3); // 10 * 3 = 30
}
}
```
**Output:**
```plaintext
parentNum * childNum1 = 10
parentNum * childNum2 = 20
parentNum * childNum3 = 30
```
**Explanation:**
* In the above-given example, we have one parent class(base class) called `BaseClass` and three subclasses that inherit the `BaseClass`.
* We created the object of each subclass. Then we multiplied the `parentNum` variable, which belongs to the parent class, with the variable of each child class.
* Even though the `parentNum` variable is absent in the child classes, we can access it quickly. **This is just because of Hierarchical Inheritance in Java.**
:::
:::section{.tip}
**Remember:** There is **no limit** on the sub-classes of one parent class. This means that any number of child classes can inherit the methods and properties of the same parent class.
:::
:::section
### Real-World Example of Hierarchical Inheritance in Java
**Code:**
```java
class Vehicle {
double basePrice = 100000;
public void showPrice() {
System.out.println("The price of Vehicle is: Rs." + basePrice);
}
}
//
class TwoWheeler extends Vehicle {
double increasePriceBy = 0.20; // 0.2 times
void finalPrice() {
basePrice = basePrice + (basePrice * increasePriceBy);
System.out.println(
"After modification, The price of bike is: Rs." + basePrice
);
}
}
// child class
class FourWheeler extends Vehicle {
double increasePriceBy = 1; // 1 times
void finalPrice() {
basePrice = basePrice + (basePrice * increasePriceBy);
System.out.println(
"After modification, The price of car is: Rs." + basePrice
);
}
}
public class Main {
public static void main(String[] args) {
// creating an Object of TwoWheeler class
TwoWheeler bike = new TwoWheeler();
// creating an Object of FourWheeler class
FourWheeler car = new FourWheeler();
// getting the base price of the vehicle
bike.showPrice();
// modifying the base price of the bike
// by referring to the base price of the vehicle.
bike.finalPrice();
// getting the base price of vehicle
car.showPrice();
// modifying the car's base price by referring to the vehicle's base price.
car.finalPrice();
}
}
```
**Output:**
```plaintext
The price of Vehicle is: Rs.100000.0
After modification, The price of bike is: Rs.120000.0
The price of Vehicle is: Rs.100000.0
After modification, The price of car is: Rs.200000.0
```
**Explanation:**
* In the above-given example, we created a parent class(base class) called `Vehicle` and two subclasses, `TwoWheeler` and `FourWheeler,` which extend the base class `Vehicle`.
* Hence, these classes can access the methods and properties of the "Vehicle" class in addition to their own.
* Here, the subclasses got access to the variable `basePrice` and the method `showPrice()` of the base class.
* We created two objects, `bike` and `car`, from the subclasses `TwoWheeler` and `FourWheeler`, respectively.
* We modified the price of the bike and car by referring to the base price of the vehicle in the base class and the `increasePriceBy` variable of each subclass, respectively. This is how we utilized the power of Hierarchical inheritance in Java.
:::
:::section{.summary}
## Conclusion
* Inheritance is one of the four pillars of **OOPs**(Object Oriented Programming System).
* Hierarchical inheritance is **one** of the types of inheritance where multiple child classes inherit the methods and properties of the same parent class.
* Hierarchical inheritance reduces the code length and increases the code modularity.
* For Hierarchical inheritance to occur, there must be at least **two or more** sub-classes that extend (or inherit) the same superclass.
* The base class is also called superclass or parent class, while the sub-classes are called child classes.
:::