Inheritance is a very important and popular concept in OOP which you utilize it in your daily coding.

Let’s examine an example to easily understand the WHAT, the WHY and the HOW of inheritance in Java programming language.

Suppose that we create a Car class as shown below:

public class Car {
	protected int numberOfWheels = 4;
	protected int numberOfSeats = 4;

	protected int length = 10;
	protected int height = 4;

	protected int enginePower = 500;

	public void start() {
	}

	public void stop() {
	}

	public void gear() {
	}

	public void turn() {
	}

	public void brake() {
	}
}

This class models a real car (abstraction) which has 4 wheels and can carry up to 4 persons. It has basic behaviors of a standard car (encapsulation) such as start, stop, gear, turn and brake.

This car can also carry a small amount of goods such as luggage and home stuffs. That’s good enough for households.

However, if you open a consumer business, you definitely need a bigger car that is able to carry much more goods and carry fewer people. You may guess it: A truck.

A truck is a car, except that it has bigger payload, stronger engine and fewer seats. In Java, we create the Truck class like this:

public Truck extends Car {
}

Here, the keyword extends is used to make a class inherits data and behaviors of another class. This is called Inheritance. The Truck class inherits all the characteristics and behaviors of a car.

The truck is bigger and stronger than the car, thus it’s logical to reset relevant characteristics in the constructor:

public Truck extends Car {
	public Truck() {
		numberOfWheels = 8;
		numberOfSeats = 2;
		enginePower = 1500;
		length = 20;
		height = 8;
	}
}

As you can see, we don’t need to rewrite all the data and behaviors of the Truck class from scratch. Instead, we make the Truck class extended the Car class in order to have all the data and behaviors of a car. That’s why inheritance comes into place: for code reusability.

The Car is called the parent class, and the Truck is called the child class.

The parent class is also called the superclass or the base class.

The child class is also called the subclass or the derived class.

 

1. What is Inheritance

So for now, you understand that inheritance is the ability of a class inherits data and behaviors from another class. Note that only public and protected members of the superclass are inherited by the subclass. The subclass can freely add new members to extend features of the superclass.

Let’s consider another example. The city where you live, needs a team of fire trucks. Basically, a fire truck is a truck with a special water tank and the ability to fire water. So you can write a FireTruck class as follows:

class FireTruck extends Truck {
	public FireTruck() {
		length = 28;
		height = 12;
		enginePower = 2000;
	}

	public void blowWater() {
	}
}

Here, as you can see, the FireTruck is bigger and stronger than a normal truck, and it has a special behavior: blowWater() to stop fire.

The characteristics of the FireTruck are reset in its constructor, and the new behavior is implemented in a new method. A FireTruck is a Truck, which is a Car, so we can have the following relationship:

Car -> Truck -> FireTruck

This is called inheritance tree which represents the parent-child relationship among classes.

 

2. Why is Inheritance?

As you can see in the above examples, inheritance is for reusing code. When you want to extend features of a class, you can write a subclass to inherit all data and behaviors of that superclass. This saves time on writing code.

Another reason for implementing inheritance is for the purpose of extensibility. It’s easier to extend a class and add new features than writing a new class from scratch.

And using inheritance promotes the maintainability of the code. Imagine you have a superclass and 5 subclasses. When you want to update a common feature of all these classes, you just update the parent class in one place. That’s much easier than updating every single class in case there is no inheritance.

 

3. How is Inheritance implemented in Java?

In Java, inheritance can be implemented in three forms:

  • A class inherits another class. The keyword extends is used, for example:
    public class Person { }
    
    public class Deisnger extends Person { }
    
    public class Programmer extends Person { }
    
    public class Architect extends Programmer { }
  • A class implements another interface. The keyword implements is used, for example: 
    public interface Wearable { }
    
    public class Shirt implements Wearable { }
    
    public class Glass implements Wearable { }

     

  • An interface inherits another interface. The keyword extends is used, for example:
    public interface Collection { }
    
    public interface List extends Collection { }
    
    public interface Set extends Collection { }

     

That’s what I want to share with you today about inheritance in Java.

 

Related Tutorials:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#1Tigran2022-02-02 01:10
I just want to thank you for the topic, it's really easy to understand.
Quote