Object-Oriented Programming (OOP) is something that you should hear about it as soon as you learn Java. This concept of programming has been making a lot of beginners turn their back and walk away from writing code.

For those who got to this far, let’s see what is Inheritance is in OOP and How it helps us programming.

Inheritance in Java Object Oriented Programming

Consider what is an object in Java programming

In an attempt to pull programming closer to the real-life object which helps programmer easily to structure their code. The concept of OOP is created.

A real-life object will have some kind of attributes and behaviors, for example, we walk into a hospital and observe. There are a lot of patients with different status. But they share something in common: first name, last name, have a health card number and need to provide the health card number to the doctor. So that we have a class of Patient with attributes: firstName, lastName,  healthCardNumber and a behavior of provideHealthNumber();

package com.samderlust;

public class Patient {
    private String firstName;
    private String lastName;
    private int healthCardNumber;

    public Patient(){
        this("unknown", "unknown", -1);
    }

    public Patient(String firstName, String lastName, int healthCardNumber) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.healthCardNumber = healthCardNumber;
    }
    
    public String provideHealthNumber() {
        return "Health Card Number " + healthCardNumber;
    }
}

Then, we have some patients who stay outside of the hospital, another group of patients is in Maternity. Those can be separate into two new class but they still share the common attributes and behavior with the class Patient. Should we write new whole classes for those? Of course No. That where we use inheritance, thus a class will inheritance attributes and behavior or more precise, variables and methods from another.

Extending from superclass

In Java, to make a class inherit from another class, we use keyword “extends“.

public class OutPatient extends Patient {
    private double distanceFromClinic;
    private boolean mobility;

    public OutPatient(String firstName, String lastName, int healthCardNumber, 
                      double distanceFromClinic, boolean mobility) {
        super(firstName, lastName, healthCardNumber);
        this.distanceFromClinic = distanceFromClinic;
        this.mobility = mobility;
    }
}

OutPatient is extended from Patient, for that we call Patient is the superclass and OutPatient is the sub-class. This sub-class can contain its own new variables and methods that fit the need of it.

The super keyword

In the precedent code, we see the keyword super in the constructor function of the sub-class. The super keyword here refer or chain to the constructor function in its superclass (Patient). This is another kind of chaining constructor from sub-class to the superclass.

Inheritance of attributes and methods

With those things in place, an object that initiated from OutPatient class have 5 attributes and 1 method.

public static void main(String[] args) {
    OutPatient justin = new OutPatient("Justin", "Micheal", 123, 10, false);
}
All the variable of the new object initiated from OutPatient class

That means, now you can call the method provideHealthNumber() that you defined in the superclass Patient inside an object that created from the sub-class OutPatient.

System.out.println(justin.provideHealthNumber());
// Health Card Number 123

Override superclass’ method

But now, you want the method provideHealthNumber() for OutPatient class must have something to indicate that this is the health card number of an Out-patient, and it will be distinct from other kinds of patients. So, you can not just refactor the code of this method inside Patient class, because it will impact all of the others sub-class.

Java gives you a way to do this, override the method that inherited from the father.

@Override
public String provideHealthNumber() {
    return " Out Patient " + super.provideHealthNumber();
}

Now, we call the method provideHealthNumber from the object of OutPatient class and see the difference.

System.out.println(justin.provideHealthNumber());
// Out Patient Health Card Number 123

I will write another topic about override and overload function. Now, just give yourself some practice with Inheritance in Java.

Remember, inheritance is extending a sub-class from a superclass. It’s totally different from initiate an object from a class.

 

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here