JAVA - Inheritance Beginner's Guide

JAVA - Inheritance Beginner's Guide

Introduction

In java, Inheritance is one of the most important concepts of Object-oriented programming (OOP). It allows us to inherit the properties and behaviors from another class, known as the super class or parent class. In simple terms, We can inherit all the properties from our parents and grandparents, right? In the same way, the child class can inherit all the properties and methods from their parent class. So, we have two classes :

  1. Parent Class (Super class or Base class)

  2. Child Class(Sub class or Derived class)

Basic Inheritance

In Java, inheritance is implemented using the 'extends' keyword. Let's consider the below example to understand how inheritance works in java.

class Car {
    // Parent Class
    public void colorOfCar() {
        System.out.println("Color of Car is black");
    }
    public void autoParkFacility() {
        System.out.println("Car is having auto parking facility");
    }
    public void autoDriveFacility() {
        System.out.println("Car is not having auto driving facility");
    }
}

class Audi extends Car{
    //Child Class
    /*All the methods which are available in the parent class are
    being inherited in child class.*/
}

public class Demo {
    public static void main(String[] args) {
        Audi audi = new Audi();
        audi.colorOfCar();
        audi.autoDriveFacility();
        audi.autoParkFacility();
    }
}

The final output will be :

In the above example, the 'Audi' class extends the 'Car' class using the extends keyword. Which means that the 'Audi' class inherits all the properties and methods of the 'Car' class such as 'colorOfCar()', 'autoDriveFacility()', 'autoParkFacility()'. In the 'Demo' class I have created the object of 'Audi' class to display the information. This is an example of Single level inheritance.

Levels of Inheritance :

  1. Single level inheritance

  2. Multi-level inheritance

  3. Hierarchical inheritance

Multi-level inheritance Example

In Java, the multi-level inheritance comes into the picture where the child class inherits from its parent and grandparent class. Let's see the example with the below code.

class Engine{
    //Grand parent class
    public void carEngine() {
        System.out.println("This is inherited from grand parent class");
    }
}

class Car extends Engine {
    //Parent Class
    public void colorOfCar() {
        System.out.println("Color of Car is yellow");
    }
    public void autoParkFacility() {
        System.out.println("Car is having auto parking facility");
    }
    public void autoDriveFacility() {
        System.out.println("Car is not having auto driving facility");
    }
}

class Audi extends Car{
    //Child Class
}
public class Demo {
    public static void main(String[] args) {
        Audi audi = new Audi();
        audi.colorOfCar();
        audi.autoDriveFacility();
        audi.autoParkFacility();
        audi.carEngine();
    }
}

In this example, the 'Audi' class extends the 'Car' class and the 'Car' class extends the 'Engine' class. So what are all the properties and methods which are available in the 'Engine' class inherited in the 'Audi' class. Similarly, we can also have 'Engine' class which can extends some other classes too. This is an example of multi-level inheritance.

The final Output will be:

Hierarchical Inheritance

It comes into the picture when multiple child classes inherit the properties and methods of the same parent class. The hierarchy of classes that results from this inheritance is a tree-like structure, with the superclass at the top and the subclasses at the bottom. Let's see the example with the below code.

class Animal{
    public void eat() {
        System.out.println("Animal is eating");
    }
}
class Cat extends Animal{
    public void catSounds() {
        System.out.println("Cat sounds like meowww");
    }
}
class Dog extends Animal{
    public void dogBarks() {
        System.out.println("Dog is barking");
    }
}
class Cow extends Animal{
    public void cowMoos() {
        System.out.println("Cow sounds ");
    }
}

public class Demo {
    public static void main(String[] args) {    
        Cat cat = new Cat();
        cat.eat();
        cat.catSounds();

        Dog dog = new Dog();
        dog.eat();
        dog.dogBarks();

        Cow cow = new Cow();
        cow.eat();
        cow.cowMoos();
    }
}

In this example, there is one parent class called 'Animal' and three subclasses which are 'Cat' 'Dog' 'Cow' having some of their own individual methods. Even though where we don't have 'eat()' method is not present in the child classes, we can access it easily. This is just because of Hierarchical Inheritance in java.

The final Output will be

Some important points to take away

  1. Child class can inherit all the properties and methods from their parent class but, parent class cannot inherit anything from their child classes.

  2. Parent class can have multiple child classes but, child class extends only one parent class.

  3. Siblings concept is not applicable in inheritance, one sibling cannot inherit from another sibling.

  4. Multiple inheritance is not allowed in the form of classes because the child class will get confused about which parent class it needs to inherit. But multiple inheritance is supported in form of interfaces. Let's see that in another blog.

"I appreciate you taking the time to read this post. I hope you found it informative!"