INHERITANCE - Java OOPs Simplified - Part 2
Let's delve deeper into one of the fundamental pillars of Object-Oriented Programming (OOP): Inheritance.

Search for a command to run...
Let's delve deeper into one of the fundamental pillars of Object-Oriented Programming (OOP): Inheritance.

No comments yet. Be the first to comment.
Artificial Intelligence (AI) is growing at lightning speed and so are the tools used to build it. If you’ve ever found Python too slow for big machine learning tasks, or felt overwhelmed by the complexity of faster languages like C++, you’re not alon...

Whenever we set up authentication and authorization using JWT (JSON Web Tokens), it's important to ensure the use of refresh tokens to enhance security and provide continuous access without requiring the user to log in frequently. Authentication vs A...

This is one of the four pillars of Object-Oriented Programming. So let's dive in detail to understand what it really is. What is Encapsulation? Just imagine a capsule pill. The medicine inside the capsule is not exposed directly. Instead, it's encaps...

When working on any project, you will encounter various situations where you need to utilize dates in different forms. Sometimes, you need to retrieve the current date, while other times you may have to schedule a process for a specific time. Additio...

ABSTRACTION In the previous articles, we delve deep into the 2 pillars of OOPs, i.e. Inheritance and Polymorphism. In this, we will understand the important things related to another important pillar of OOPs, i.e. Abstraction. So the first question i...

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.
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).
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.
Single Inheritance
Multilevel Inheritance
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 means that a class can inherit from only one superclass. In other words, a subclass can have only one immediate parent class.
class Parent {
// Parent class code
}
class Child extends Parent {
// Child class inherits from Parent
}

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.
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 happens when multiple classes inherit from a single superclass, which means that multiple child classes will have a common parent class.
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.