Inheritance in Java

Inheritance is one of the fundamental concepts of object-oriented programming (OOP) in Java. It allows a class to inherit the properties and behaviors of another class. In this tutorial, we will discuss what inheritance is in Java and how to implement it with examples.

Inheritance in Java

Inheritance is the mechanism of creating a new class from an existing class. The new class, called the subclass or derived class, inherits the properties and behaviors of the existing class, called the superclass or base class. This allows the subclass to reuse the code from the superclass, making the code more organized and easier to maintain.

In Java, we use the keyword “extends” to create a subclass that inherits from a superclass. The subclass can access the public and protected members of the superclass, but it cannot access the private members.

UML class diagram that represents Java inheritance for classes:

Inheritance in Java

In this diagram, the superclass “Superclass” has a private field called “field1” of type “int” and a public method called “method1” that returns “void”. The subclass “Subclass” inherits the field and method from the superclass and also has a private field called “field2” of type “String” and a public method called “method2” that returns “void”.

Let’s look at an example to see how inheritance works in Java.

public class Animal {
    public void eat() {
        System.out.println("Animal is eating");
    }
}

public class Dog extends Animal {
    public void bark() {
        System.out.println("Dog is barking");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();
        dog.bark();
    }
}

In this example, we have defined two classes – “Animal” and “Dog”. The “Animal” class has a public method called “eat”, while the “Dog” class has a public method called “bark”. The “Dog” class extends the “Animal” class using the “extends” keyword, which means it inherits the “eat” method from the “Animal” class.

In the “Main” class, we create an object of the “Dog” class and call its “eat” and “bark” methods. Notice that we are able to access the “eat” method of the “Animal” class through the “Dog” class, even though we did not define it in the “Dog” class.

Overriding Methods

In Java, the subclass can override the methods of the superclass to provide its own implementation. This allows the subclass to customize the behavior of the inherited methods.

Let’s look at an example to see how method overriding works in Java.

public class Animal {
    public void eat() {
        System.out.println("Animal is eating");
    }
}

public class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("Dog is eating");
    }
    
    public void bark() {
        System.out.println("Dog is barking");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();
        dog.bark();
    }
}

In this example, we have overridden the “eat” method of the “Animal” class in the “Dog” class. We have provided our own implementation of the “eat” method that prints “Dog is eating” instead of “Animal is eating”. When we call the “eat” method on the “Dog” object, it calls the overridden method in the “Dog” class instead of the inherited method in the “Animal” class.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author