Java OOPs Simplified - Part 2

Java OOPs Simplified - Part 2

Let's delve deeper into one of the fundamental pillars of Object-Oriented Programming (OOP): Inheritance.

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

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

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

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.