Java Inheritance

In this lesson of Java course, we will learn about the Java Inheritance. If you’re new to Java programming, you might have heard the term “inheritance” before. Inheritance is a key concept in object-oriented programming that allows you to create new classes based on existing classes. In this blog post, we will explore the basics of Java inheritance, including its syntax and benefits.

1. What is Java Inheritance?

Java inheritance is a mechanism that enables a new class to be based on an existing class. Inheritance allows you to create a new class that shares the properties and methods of an existing class. In other words, inheritance is the process of creating a new class by inheriting the properties and behaviors of an existing class. Let’s take the following example for better understanding of what is Java Inheritance?

  • We have a class name car.
  • There are multiple brands of cars in market e.g. Audi, BMW, Honda etc.
  • Car define a certain set of features which are common across all brands.
  • Every car brand (e.g. Audi, BMW etc.) is a car and carry the common features defined by the Car (e.g. engine, ABS, ABS etc. ).
  • Its safe to say every card bard inherits the core feature defined in the car.

Let’s take a closer look:

class Car {

    // field and method of the parent class
    String name;
    String engine;

    public void display() {
        System.out.println("Welcome to the world of automobile!!");
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEngine() {
        return engine;
    }

    public void setEngine(String engine) {
        this.engine = engine;
    }
}

// inherit from Car
class Audi extends Car {

    // new method in subclass
    public void displayName() {
        System.out.println("car name is " + name);
    }
}

// inherit from Car
class BMW extends Car {

    // new method in subclass
    public void displayName() {
        System.out.println("car name is " + name);
    }
}

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

        // create an object of the subclass
        Audi audi = new Audi();

        // access field of superclass
        audi.name = "Audi";
        audi.displayName();

        BMW bmw = new BMW();
        bmw.name = "BMW";
        bmw.displayName();

        // call method of superclass
        // using object of subclass
        audi.display();
        bmw.display();
    }
}

Here is the output of the program:

car name is Audi
car name is BMW
Welcome to the world of automobile!!
Welcome to the world of automobile!!

Let’s try to understand few important points in the above code

  • We create object of the Audi class and called the displayName() and display() method.
  • If you look closely field name is part of the Car class.
  • Our Audi class inherits the Car class, so it inherits the field and method defined in the parent class.
  • Same thing apply to the display() method which is defined in the Car class and every class which inherits Car will have access to this method.
Java Inheritance
Java Inheritance

1.1. Java is-a Relation

Java inheritance is also known as is-a relation. Inheritance only exists between 2 classes in Java if there we have is-a relation. Let’s look at the above example to understand is-a relation in Java Inheritance

  • Car is a Vehicle.
  • Audi is a Car
  • BMW is a car.
  • Cat is an animal.
Inheritance in Java is not different than the real world in inheritance. You can’t define Inheritance in real life without a relation, the same holds true in Java.

2. How to Use Inheritance in Java

To use inheritance in Java, you need to use the “extends” keyword. Here’s a basic syntax for creating a subclass that inherits from a super-class.

class BMW extends Car {
}

In this example, the “Subclass” is a new class that inherits the properties and methods of the “Superclass.” The “extends” keyword is used to indicate that the “Subclass” is derived from the “Superclass.”

3. Types of Inheritance

Java supports several types of inheritance:

  • Single Inheritance: A subclass that inherits from a single super-class.
  • Multiple Inheritance: A subclass that inherits from multiple super-classes. (Note: Java doesn’t support multiple inheritance of classes, only interfaces)
  • Multi-level Inheritance: A subclass that inherits from another subclass.

3.1. Single Inheritance

As we mentioned earlier, a single inheritance in Java is defined by a subclass that inherits from a single super-class. Car example in our section 1 is a perfect example of single inheritance

class Car {

    // field and method of the parent class
    String name;
    String engine;

    public void display() {
        System.out.println("Welcome to the world of automobile!!");
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEngine() {
        return engine;
    }

    public void setEngine(String engine) {
        this.engine = engine;
    }
}

// inherit from Car
class Audi extends Car {

    // new method in subclass
    public void displayName() {
        System.out.println("car name is " + name);
    }
}

In above case, our Audi class has only a single parent class as Car.

3.2. Multiple Inheritance

In multiple Inheritance, a subclass that inherits from multiple super-classes.  Java doesn’t provide support for multiple inheritance in classes. We can still do multiple inheritance using the Java interfaces.

interface Vehicle {

    // abstract class
    public void type();
}

class Car {

    public void display() {
        System.out.println("4 Wheel Vehicle");
    }
}

// Audi extends Car class
// Audi implements Vehicle interface
class Audi extends Car implements Vehicle {
    
}

3.3. Multi-level Inheritance

When a class extends a class, which extends anther class then this is called multilevel inheritance. Let’s take our car example to understand the multi-level inheritance.

  • Audi extends Car.
  • Audi Q5 extends the Audi.
  • Audi Q7 extends Audi
class Car {

    public void display() {
        System.out.println("4 Wheel Car");
    }
}

class Audi extends Car {
    
}

class AudiQ5 extends Audi {
    
}

class AudiQ7 extends Audi {
    
}

4. Java Inheritance and super keyword

We can use the super keyword to call the method of the parent class from the method of the child class. This is specially helpful when we have defined the same method in both child and parent class. The super keyword can provide a lot clarity in such cases.

class Car {

    // method in the superclass
    public void name() {
        System.out.println("Its a generic car");
    }
}

// Audi inherits Car
class Audi extends Car {

    // overriding the eat() method
    @Override
    public void name() {
        // call method of superclass
        super.name();
        System.out.println("Welcome to Audi World!!");
    }

    // new method in subclass
    public void greeting() {
        System.out.println("Welcome to Q5.");
    }
}

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

        // create an object of the subclass
        Audi car = new Audi();

        // call the eat() method
        car.name();
        car.greeting();
    }
}

When we run this above code, this is how the output look like:

Its a generic car
Welcome to Audi World!!
Welcome to Q5.

5. Benefits of Inheritance

Inheritance is a powerful feature in Java that provides several benefits. Let’s take a look at some of the benefits of Java inheritance.

  • Reusability: Inheritance allows you to reuse code by inheriting properties and methods from existing classes.
  • Extensibility: Inheritance enables you to extend the functionality of an existing class by adding new properties and methods.
  • Polymorphism: Inheritance enables you to use objects of the subclass in place of objects of the super-class.

Summary

Inheritance is a fundamental concept in object-oriented programming that enables you to create new classes based on existing classes. In Java, you can use the “extends” keyword to create a subclass that inherits the properties and methods of a super-class.

Inheritance provides several benefits, including reusability, extensibility, and polymorphism. As you continue your journey in Java programming, you will encounter inheritance frequently, and you will realize its importance in building complex applications. The source code of our Java course is available on the GitHub repository.

Scroll to Top