Skip to content

Java Inheritance | Types of Inheritance | Extends Class with Examples

  • by

 Java Inheritance is the concept of Object-Oriented Programming (OOPs). Where an Object (class or Interface) acquires the property(Methods, Variables, etc) of another object. It’s also called Java extends the class.

So the idea behind this concept is the usability of code, which means when you create a new class (child class). This class extends another class (Parent class) and reuses methods, variables, and fields of the parent class. So you don’t need to write the same code again and again.

Java Inheritance Types of Inheritance Extends Class with Examples

Inheritance represents the IS-A relationship or can call a parent-child relationship. In this tutorial, you will learn all about inheritance with how-to class and interfaces use on different types of Inheritance in Java.

Types of inheritance in Java

Here are 5 types of Inheritance we will learn with an example of this tutorial.

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

Syntax

The keyword used extends (for class) and Implements (for Interface) in Java Inheritance.

For class extends keyword

class Super {
   .....
}
class Sub extends Super {
   .....
}

For Interface implements keyword

public interface SuperInterface {

}

class Hello implements SuperInterface {

}

Important Terms in Inheritance

  • Class: class is the blueprint of an object, which has a common property.
  • Extends: extends are used to achieve Inheritance in SubClass.
  • Implements: implements come when you want Inherit Interface.

Let’s start to Learn Java inheritance and Build examples

Java Inheritance and its Types with examples

If looking as class bases Inheritance there are three types of Inheritance – single, multilevel and hierarchical.

And about multiple and hybrid inheritance is supported through interface only in Java.

Single Inheritance

Example of Single Inheritance in Java with code

A Single Inheritance is a basic inheritance, where one class (subClass) inherits the features of one another class (superClass). Here is an example of Single Inheritance in Java with code.

class Vehicle {
    void run(){
        System.out.println("Running");
    }
}

class TwoWheeler extends Vehicle {
    void wheel(){
        System.out.println("2 Wheeler");
    }
}

class InheritancesExamples{
    public static void main(String args[]) {
        //Creating object
        TwoWheeler twoWheeler = new TwoWheeler();
        twoWheeler.run();
        twoWheeler.wheel();

    }
}

Output: Running
2 Wheeler

Multilevel Inheritance

Example of Multilevel Inheritance in Java

The Multiple Inheritance Concept is very required to understand  Inheritance. So In this time, one class inherits Another class and this class inherits Another one class property or features.

Grandfather -> Parent -> child

Let’s check this example of Multilevel Inheritance in Java.

class Vehicle {
    void run(){
        System.out.println("Running");
    }
}

class TwoWheeler extends Vehicle {
    void wheel(){
        System.out.println("2 Wheeler");
    }
}

class MotorBike extends TwoWheeler{
    void fuel(){
        System.out.println("Fuel Required : Petrol");
    }
}

class InheritancesExamples{
    public static void main(String args[]) {
        //Creating object
        MotorBike motorBike = new MotorBike();
        motorBike.run();
        motorBike.wheel();
        motorBike.fuel();

    }
}

Output: Running
2 Wheeler
Fuel Required: Petrol

Hierarchical Inheritance

Example code of Hierarchical Inheritance in Java

When a many class (subclass) inherit the common property of another class (Superclass), is called Hierarchical Inheritance.

Here is an example code of Hierarchical Inheritance in Java.

class Vehicle {
    void run(){
        System.out.println("Running");
    }
}

class TwoWheeler extends Vehicle {
    void wheel(){
        System.out.println("2 Wheeler");
    }
}

class FourWheeler extends Vehicle{
    void wheel(){
        System.out.println("4 Wheeler");
    }
}

class InheritancesExamples{
    public static void main(String args[]) {

        FourWheeler fourWheeler = new FourWheeler();
        fourWheeler.run();
        fourWheeler.wheel();

        TwoWheeler twoWheeler = new TwoWheeler();
        twoWheeler.run();
        twoWheeler.wheel();
    }
}

Output: Running
4 Wheeler
Running
2 Wheeler

Multiple Inheritance

Where a class (child class)  inherits properties of more than one class (parent class) is called Multiple Inheritance. But it not supported in Java.

Java Multiple Inheritance is supported by interfaces. So by definition single Inheritance can inherit multiple Interface.

How to Multiple Inheritance in Java using an interface example.

interface A {
    void doSomething();
}

interface B {

    void doSomething();
}

public interface InterfaceExample extends A, B {

    void doSomething();

}

Hybrid Inheritance

How Hybrid Inheritance in Java

As per the upper image you can see how Hybrid Inheritance in Java can implement. From Level 1 to 2 is easy but when it’s come to level 3 (Class D). That time you have to know Java not support an Inherit a single class inherits the property of many classes. To get it done you have to use an Interface.

QA: Why use inheritance in Java?

This is an important question in Interview, it can also be called “What is the advantage of Inheritance in Java”?

Here are some reasons why use it:-

  • Most Important is code Usability.
  • Efficient to use, while writing code can increase the project development speed.
  • In OOPs concept – For Method Overriding
  • Make global changes to derived (child) classes by changing a base (parent) class.

QA: Why Multiple Inheritances are not supported in Java?

The problem with multiple inheritances is that two classes may define methods that have different ways of doing the same thing, and the subclass can’t choose which one to pick. It’s called the data ambiguity in Programming terms.

Here is an example if you did Multiple inheritances, then the compiler will throw an error.

multiple inheritance not supported java compiler time error

Do comment if you have any doubts and suggestions on this tutorial. This tutorial is targeted to java beginners.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All  Inheritance in Java with example programs PDF are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading