# INHERITANCE - Java OOPs Simplified - Part 2

In my prior article, I explained the essential Object-Oriented Programming (OOP) concepts, including Java classes and objects. Now, let's embark on a comprehensive exploration of OOP's four pillars: Inheritance, Polymorphism, Abstraction, and Encapsulation.

### Inheritance

So what's meant by Inheritance in Java?

If I take an example from real life, inheritance means a child acquires some characteristics or behaviour of their parents. Right? It's almost the same in the case of Java.

In Java, inheritance is **a mechanism that allows a class to inherit or acquire the properties and behaviour(attributes and methods) of another class**. Here a new class (the child or subclass) can reuse and extend the attributes and methods of an existing class (the parent or superclass).

### Advantages of Inheritance

**Code reusability**\- We don't need to rewrite or duplicate code that already exists in the parent class.

**Easy Maintenance**\- When we make some changes in the parent class, that will be automatically inherited by all the child classes.

**Extensibility**\- We can extend the behaviour of the existing child classes by adding new fields and methods or by modifying existing ones.

**Consistency**\- It provides consistency for the code base, which makes it easier to understand and maintain.

### Types of Inheritance in Java

1. Single Inheritance
    
2. Multilevel Inheritance
    
3. Hierarchical Inheritance
    

Java also provide support for other 2 inheritances, i.e. Multiple inheritance and Hybrid Inheritance, but not directly instead it supports it through interfaces.

### Single Inheritance

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696568144753/02ede684-d003-43b1-a69f-60fd4e1cffdf.png align="center")

Single inheritance means that a class can inherit from only one superclass. In other words, a subclass can have only one immediate parent class.

```java
class Parent {
  // Parent class code
}

class Child extends Parent {
  // Child class inherits from Parent
}
```

### Multilevel Inheritance

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696573242688/0c3f6da1-fd6c-4627-82a0-1db862aaa5b7.png align="center")

Multilevel inheritance occurs when a class inherits from another class, and that class is further inherited by another class. So it's a chain of inheritance.

```java
class Grandparent {
  // Grandparent class code
}

class Parent extends Grandparent {
  // Parent class inherits from Grandparent
}

class Child extends Parent {
  // Child class inherits from Parent
}
```

### Hierarchical Inheritance

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696575320231/1884aad5-c127-4132-846e-076eb60257bd.png align="center")

Hierarchical inheritance happens when multiple classes inherit from a single superclass, which means that multiple child classes will have a common parent class.

```java
class Parent {
  // Parent class code
}

class Child1 extends Parent {
  // Child1 class inherits from Parent
}

class Child2 extends Parent {
  // Child2 class inherits from Parent
}
```

So these are the 3 types of inheritance supported by Java. Now we have two more, Multiple inheritance and Hybrid Inheritance. One crucial aspect worth noting is multiple inheritance, a concept achieved in Java through the use of interfaces. I will delve into this topic extensively in the upcoming article.
