You are currently viewing Inheritance in Java: Building on Basics for Powerful Programming

Inheritance in Java: Building on Basics for Powerful Programming

1. Introduction

In this tutorial, you will explore the basics of Inheritance in Java programming and gain hands-on experience implementing this fundamental concept.
In this tutorial, you will be using IntelliJ IDEA.

2. Prerequisites

To complete the following tutorial, you will need :

  • The Java SE Development Kit 17 (JDK 17): If you don’t have it already refer to this tutorial for the installation procedure.
  • IntelliJ IDEA: If you don’t have it already refer to this tutorial for the installation procedure.

3. What is Inheritance?

Inheritance is one of the four fundamental principles of object-oriented programming. It allows object A to inherit the attributes and methods of object B without having to declare them explicitly. Inheritance models an “is-a” type relationship, but more generally inheritance contributes to the factorization of the code.

4. Why Do You Need Inheritance?

As a developer, you don’t want to write the same piece of code repeatedly. There is indeed a principle in programming which is to avoid repeating yourself: Don’t Repeat Yourself (DRY). Inheritance will therefore make it easier for you to reuse code by allowing you to group shared code in a parent class and make it available to child classes.

5. An Example of Inheritance

Inheritance is implemented in the code below. The Animal class includes the eat() method that is common to all animals. Each animal can then inherit from this class and implement specific methods like makeSound().

class Animal{
    protected void makeSound(){
        System.out.println("I don't know. Each animal should define its own sound");
    }
    protected void eat(){
        System.out.println("Animal is eating");
    }
}

class Cat extends Animal{
    protected void makeSound(){
        System.out.println("Meow Meow");
    }
}

class Dog extends Animal{
    protected void makeSound(){
        System.out.println("Wouf Wouf");
    }
}

6. Terms Used In Inheritance

When you hear about inheritance, the following terms will come up very often: parent class, child class, override, and single inheritance.

6.1. Parent Class

The parent class, also called superclass, is the class into which you will group shared code. It’s an ordinary Java class with no particular features. However, all inheritable attributes and methods must have the “protected” access modifier. If they have “private” as their access modifier, they won’t be accessible to the child classes.

class Person{
    protected String firstName;//This protected field can be inherited
    protected String lastName;//This protected field can be inherited
    private Date dateOfBirth;//This private field can't be inherited

    protected void setFirstName(String firstName){//This protected method can be inherited
        this.firstName = firstName;
    }
    protected void setLastName(String lastName){//This protected method can be inherited
        this.lastName = lastName;
    }
    private void setDateOfBirth(Date dateOfBirth){//This private method can't be inherited
        this.dateOfBirth = dateOfBirth;
    }
}

6.2. Child Class

The child class is the one that wants to reuse the code shared in a parent class. It is then said that the child class inherits from the parent class. In the header of its declaration, the child class will use the term extends SuperClassName to indicate that it inherits from the class “SuperClassName”. When you need to access a member of the parent class from the child class, you will use the keyword super.

class Student extends Person{

    void displayFieldsValues(){
        System.out.println(super.firstName);//Accessing field from parent class using super
        System.out.println(super.lastName);//Accessing field from parent class using super
        System.out.println(super.dateOfBirth);//This will not compile. The private field is not visible
    }

    void setFields(){
        super.setFirstName("John");//Accessing method from parent class using super
        super.setLastName("Doe");//Accessing method from parent class using super
        super.setDateOfBirth(new Date());//This will not compile. The private method is not visible
    }
}

6.3. Override

When a child class inherits a method already implemented in the parent class, it has the choice of modifying it to include specificities or not. The act of reimplementing a method already implemented in the parent class is called overriding. You can read more about methods overriding in this post.

6.4. Single Inheritance

Java, unlike other programming languages, doesn’t allow multiple inheritance in classes. That is to say, a class cannot inherit from multiple other classes.
The code below will not compile:

class ClassA extends ClassB, ClassC{//Will not compile

}
class ClassB{
    void methodB(){
        System.out.println("I'm in Class B");
    }
}
class ClassC{
    void methodC(){
        System.out.println("I'm in Class C");
    };
}

A workaround to this constraint is the use of interfaces.

class ClassA implements InterfaceB, InterfaceC {

    @Override
    public void methodB() {
        System.out.println("Method from InterfaceB to be implemented");
    }

    @Override
    public void methodC() {
        System.out.println("Method from InterfaceC to be implemented");
    }
}
interface InterfaceB{
    void methodB();
}
interface InterfaceC{
    void methodC();
}

7. Conclusion

In this tutorial, you learned the Concept of Inheritance in Object-oriented Programming.
You saw some use cases of Inheritance in Java as well as important keywords when it comes to Inheritance.

8. References

1) OCP Oracle Certified Professional Java SE 17 by Khalil A. Mughal and Vasily A. Strelnikov
2) Oracle Java Documentation

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.

This Post Has 6 Comments

  1. Donald Hendrix

    Whoa! This blog looks just like my old one! It’s on a totally different subject but it
    has pretty much the same layout and design.
    Outstanding choice of colors!

  2. Charles Dao

    It is the best time to make a few plans for the longer term and
    it’s time to be happy. I have learn this publish and if
    I could I desire to recommend you few interesting things or advice.
    Maybe you could write next articles referring to this article.
    I wish to learn even more things about it!

    1. Noel Kamphoa

      Hi Charles,
      Can you be more specific?

  3. avenue17

    Just that is necessary. An interesting theme, I will participate.

Comments are closed.