DEV Community

Raj
Raj

Posted on Edited on

Inheritance type in java

Introduction

When we start learning Java we come across many important concepts in Object Oriented Programming (OOP). One of those concepts is Inheritance.

Inheritance is a concept that allows one class to acquire the properties and methods of another class. In simple words instead of writing the same code again and again we can create one common class and allow other classes to reuse its functionality.

We can understand inheritance using a real world family example. A child may inherit certain characteristics from their parents. Similarly in Java a child class can inherit properties and methods from a parent class.

What Is Inheritance in Java?

Inheritance is an Object-Oriented Programming concept in which one class acquires the properties and methods of another class.

The class that provides its properties and methods is called the Parent Class or Superclass. The class that inherits those properties and methods is called the Child Class or Subclass.

Example imagine a father has a house. His son wants to use that property. in Java the Son class can inherit the properties and methods of the Father class.

class Father {

    void house() {
        System.out.println("Father has a house");
    }
}

class Son extends Father {

    void bike() {
        System.out.println("Son has a bike");
    }
}

public class Main {

    public static void main(String[] args) {

        Son obj = new Son();

        obj.house();
        obj.bike();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Father has a house
Son has a bike
Enter fullscreen mode Exit fullscreen mode

The Father class contains the house() method. The Son class uses the extends keyword to inherit the Father class.

When we create an object of the Son class, we can access both the inherited house() method and its own bike() method.

** Why Do We Use Inheritance?**

Imagine you are developing a school management application. In that application you need to create three classes: Student Teacher and Staff.

All three classes have some common information such as name, age, and address.

Without inheritance we might write the same properties in all three classes. This creates code duplication.

Instead, we can create one common class called Person and allow Student, Teacher and Staff to inherit it.

Example:

class Person {

    String name;
    int age;

    void display() {
        System.out.println(name);
        System.out.println(age);
    }
}

class Student extends Person {

}

class Teacher extends Person {

}

class Staff extends Person {

}
Enter fullscreen mode Exit fullscreen mode

Now Student, Teacher, and Staff can reuse the common properties and methods from the Person class.

The main advantages of inheritance are:

  1. Code Reusability – We can reuse existing code instead of writing it again.
  2. Code Duplication Reduction – Common properties and methods can be maintained in one parent class.
  3. Easy Maintenance – Changes to common functionality can be managed in one place.
  4. Extensibility – We can create new child classes using existing parent classes.
  5. Method Overriding – Child classes can provide their own implementation of inherited methods.

Types of Inheritance in Java

Inheritance can be implemented in different ways depending on the relationship between classes.

Java commonly discusses five types of inheritance:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheritance
  5. Hybrid Inheritance

1.Single Inheritance

Single inheritance means one child class inherits from one parent class.

For example, imagine a Father has a house and a Son inherits the property.

Java Example:

class Father {

    void property() {
        System.out.println("Father has a house");
    }
}

class Son extends Father {

    void vehicle() {
        System.out.println("Son has a car");
    }
}

public class Main {

    public static void main(String[] args) {

        Son obj = new Son();

        obj.property();
        obj.vehicle();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Father has a house
Son has a car
Enter fullscreen mode Exit fullscreen mode

Here the Son class inherits the Father class. Therefore, the Son object can access the inherited property() method and its own vehicle() method.

This relationship between one parent and one child is called Single Inheritance.

2. Multilevel Inheritance

Multilevel inheritance means one class inherits another class, and a third class inherits the second class.

We can understand this through a family relationship:

Grandfather → Father → Son.

The Father inherits from Grandfather and the Son inherits from Father.

Java Example:

class Grandfather {

    void land() {
        System.out.println("Grandfather has land");
    }
}

class Father extends Grandfather {

    void house() {
        System.out.println("Father has a house");
    }
}

class Son extends Father {

    void bike() {
        System.out.println("Son has a bike");
    }
}

public class Main {

    public static void main(String[] args) {

        Son obj = new Son();

        obj.land();
        obj.house();
        obj.bike();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Grandfather has land
Father has a house
Son has a bike
Enter fullscreen mode Exit fullscreen mode

Here the Son class directly inherits Father but it can also access the inherited method from Grandfather because Father already inherits Grandfather.

3. Hierarchical Inheritance

Hierarchical inheritance means multiple child classes inherit from one common parent class.

For example one parent has two children: a Son and a Daughter.

Both children can inherit common properties from their parent but they can also have their own individual properties.

Java Example:

class Parent {

    void property() {
        System.out.println("Parent has property");
    }
}

class Son extends Parent {

    void bike() {
        System.out.println("Son has a bike");
    }
}

class Daughter extends Parent {

    void car() {
        System.out.println("Daughter has a car");
    }
}

public class Main {

    public static void main(String[] args) {

        Son s = new Son();
        Daughter d = new Daughter();

        s.property();
        s.bike();

        d.property();
        d.car();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Parent has property
Son has a bike
Parent has property
Daughter has a car
Enter fullscreen mode Exit fullscreen mode

Here both Son and Daughter inherit the same Parent class.

The Son can access the parent's property and his own bike method. Similarly, the Daughter can access the parent's property and her own car method.

4.Multiple Inheritance

Multiple inheritance means one child class inherits from more than one parent class.

For example imagine a child wants to access properties from both Father and Mother.

However Java does not support multiple inheritance through classes.

Why? Imagine both parent classes have a method with the same name. If the child inherits both classes it may become confusing to decide which parent's method should be executed. This situation is related to the Diamond Problem.

However Java provides another way to achieve multiple inheritance using interfaces.

Java Example:

interface Father {

    void house();
}

interface Mother {

    void car();
}

class Child implements Father, Mother {

    public void house() {
        System.out.println("Father has a house");
    }

    public void car() {
        System.out.println("Mother has a car");
    }
}

public class Main {

    public static void main(String[] args) {

        Child obj = new Child();

        obj.house();
        obj.car();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Father has a house
Mother has a car
Enter fullscreen mode Exit fullscreen mode

Here the Child class implements two interfaces: Father and Mother.

Java allows a class to implement multiple interfaces. This is how we achieve Multiple Inheritance in Java.

5.Hybrid Inheritance

Hybrid inheritance means combining two or more types of inheritance.

For example, we can combine hierarchical inheritance and multiple inheritance.

Java does not support hybrid inheritance using classes alone when it requires multiple class inheritance.

However we can achieve hybrid inheritance by combining classes and interfaces.

This is useful when an application needs to reuse functionality from different types of relationships.

Difference Between All Types of Inheritance

Inheritance Type Meaning
Single One child inherits one parent
Multilevel One class inherits another class in a chain
Hierarchical Multiple children inherit one parent
Multiple One child inherits multiple parents
Hybrid Combination of two or more inheritance types

In Java, Single, Multilevel and Hierarchical inheritance are supported through classes. Multiple inheritance is supported through interfaces and Hybrid inheritance can be achieved using classes and interfaces together.

** Important Keywords Used in Inheritance**

When working with inheritance in Java two important keywords are extends and implements.

extends: We use the extends keyword when one class inherits another class.

class Son extends Father {

}
Enter fullscreen mode Exit fullscreen mode

implements: We use the implements keyword when a class implements an interface.

class Child implements Father, Mother {

}
Enter fullscreen mode Exit fullscreen mode

Remember:

  • Class to class – extends
  • Class to interface – implements
  • Interface to interface – extends

** Where Is Inheritance Used in Real Time Projects?**

Inheritance is not only used for learning Java concepts. Developers also use it when building real world applications.

For example consider an online banking application.

A banking application may have different types of accounts:

  • Savings Account
  • Current Account
  • Salary Account

All these accounts have some common features such as account number, account holder name, deposit and withdrawal.

Instead of writing the same code in every account class, developers can create a common BankAccount class.

Then Savings Account, Current Account, and Salary Account can inherit the common properties and methods.

This makes the application easier to develop, understand, and maintain.

Top comments (0)