Inheritance In Java Object Oriented Programming (Oop) With Example. - FreeCodeBlog

Last updated: Jan 04, 2022

Inheritance is an important part in java object oriented programming (OOP). When we need to use all the property, method, and behavior of a class into another class then we have to use the concept of inheritance in java . There are 2 types of relationship available in the section of Inheritance in java.And those are “IS-A” and Has-A (Association). So let’s see an example of them one by one.

 

IS-A Relation:

Let’s suppose a java class named Vehicle which contains some attribute called Company, Model, Color and a show method to display all the information.

 

//Vehicle Class
public class Vehicle {

    protected String Company, Model, Color;

    public Vehicle(String company, String model, String color) {
        Company = company;
        Model = model;
        Color = color;
    }

    public void show(){
        System.out.println(Company);
        System.out.println(Model);
        System.out.println(
        
        
        Color);

    }
}

 

And there is another class named Car which has also some properties like numberOfSeat, numberOfDoor and a show method to print all that.

 

//Car Class
public class Car extends Vehicle{
    private int numberOfseat,numberOfDoor;

    private Engine engine;


    public Car(String company, String model, String color, int numberOfseat, int numberOfDoor,Engine engine) {
        super(company, model, color);
        this.numberOfseat = numberOfseat;
        this.numberOfDoor = numberOfDoor;
        this.engine = engine;
    }

    public void show(){
        System.out.println(Company);
        System.out.println(Model);
        System.out.println(Color);
        System.out.println(numberOfseat);
        System.out.println(numberOfDoor);
        engine.show();

    }
}

 

//Main class

public class Main {

    public static void main(String[] args) {
        Car c1 = new Car("BMW","B-35","Red",4,5);
        c1.show();
    }
}

So now if we want to have all the attribute and method of Vehicle class into Car class then we need use “extends” keyword which is the syntax of inheritance in java. As you can see in the Car class we have used “Car extends Vehicle” that means we have successfully inherited all the property (Company, Model, Color and show() method) in Car class. Now we can able to make any kind of operation in Car class by using those property. This is what called inheritance in java and the relationship between Vehicle and Car class called “IS-A” relation.

 

Output: 

BMW
B-35
Red
4
5

 

HAS-A Relation:

 

Alright now let’s add there another class Called Engine. Which have NumberOfcc, mileage as attribute and a show method. If we want when the Car class will run, all the info of Engine should print with previous output as a final output of Car class. Then we have to create a reference variable of Engine class and pass it throughout the constructor. This is also a way to use inheritance in java.

 

//Engine Class
public class Engine {
    private int numberOfcc;
    private double mileage;

    public Engine(int numberOfcc, double mileage) {
        this.numberOfcc = numberOfcc;
        this.mileage = mileage;
    }

    public void show(){
        System.out.println(numberOfcc);
        System.out.println(mileage);
    }
}

 

//Main class
public class Main {

    public static void main(String[] args) {

        Engine en = new Engine(160,80.30);
        Car c1 = new Car("BMW","B-35","Red",4,5,en);
        c1.show();
    }
}

 

If you have a look on Car class you can see we are passing Engine class through the constructor. And in Main class first we initialize an object of Engine class then passing reference variable of  Engine class through Car class object.  By how all the info of Engine class will print when we run our Car class. So this is also an Example of Inheritance in java and the relationship between Car and Engine class called “HAS-A” relation.

 

Output:

BMW
B-35
Red
4
5
160
80.3

 

 

Alright so we have done a lots of discussion today about inheritance in java. Let’s finish today’s session by seeing a few advantages of using Inheritance in java:

  1. Code Reusability
  2. Cost Cutting
  3. Reduce Redundancy
  4. Less Code

That’s it for today. Hope you got something from this session. Have a good day:-)

Post Tags
Post Writer
rabbi2437
Rabbi Islam (@rabbi2437)

My name is Rabbi Islam. I'm a Java Programmer. I live in Bangladesh and I love to write tutorials that can help to other programmer.

Related Posts
Jdk Installation Steps For Java And Android Studio In Windows 10

JDK (Java Development Kit) creates an environment for user to execute java programs. So basically if you want to run your java program, JDK has to... Read More

Toast In Android Studio

 

What is toast in android studio: 

Toast is nothing but a feedback or a simple pop up message when a... Read More