Inheritance in Java ( Is-A relationship)

Inheritance is one of the pillars of the OOP ( Object Oriented Programming), and it is almost everywhere in Java. Inheritance is the mechanism through which a class can inherit another class’s properties ( variables and methods ) and saves us from writing the code again for the new class.

Before starting, let’s get familiar with some terminologies

  • Super Class – SuperClass is the class whose properties will be inherited by another class. It is also called parent class or base class.
  • Sub Class – A subclass is the class that will inherit the properties of another class. It is also called child class or derived class.

Now let’s understand what is inheritance and what is IS-A relationship?

In OO ( object-oriented), the concept of IS-A is based on class inheritance or interface implementation. IS-A is a way of saying, “this thing is a type of that thing”. For example, Apple is a type of fruit, so in OO terms, we can say that “Apple IS-A fruit”, Car IS-A vehicle, Broccoli IS-A Vegetable (not a very fun one, but it still counts).

We express the IS-A relationship in Java using two keywords

  • extends ( for class inheritance)
  • implements ( for interface implementation)

As a Car is a type of Vehicle, we can make the below statements about car and vehicle in terms of inheritance and terminologies.

  • Car is a subclass of Vehicle.
  • The Vehicle is the superclass of Car.
  • Car is derived from Vehicle.
  • The vehicle is the base class for Car.

Returning to the IS-A relationship, the below statement is also true about inheritance.

Car extends Vehicle means Car IS-A Vehicle.

What is the need for an inheritance, and why do we need it?

The two most common reasons to use inheritance are –

  • To promote code reuse
  • To use polymorphism

Code reuse is a good practice while writing code, saving you from writing redundant code in your project. Don’t worry if you don’t understand it yet. You will come to know about it while writing super awesome code yourself.

The syntax for using inheritance in Java

extends keyword is used to achieve the class inheritance in Java. If we take the example of the Car IS-A vehicle, then in inheritance terms, it would be written like this –

public Car extends Vehicle{
   // some code
}

The above code represents inheritance or IS-A relationship between two classes ( or entities ).

Code example of Inheritance

Now, let’s create a simple class ( say Base ) with a method named display() as shown below.

public class Base {

	public void display() {
		System.out.println("display method in base class");
	}

}

And now, let’s make a subclass of the Base class, which will inherit the properties of the Base class.

public class Derived extends Base {

}

Now, the Derived class also has the display() method, despite not writing a single piece code inside the Derived class, and below is the code proving the same.

public class Codekru {

	public static void main(String[] args) {
		Derived derived = new Derived();
		derived.display();
	}
}

Just run the above class and see what happens.

display method in base class

So, this is how inheritance works in Java. Easy, right 🙂

Constructor Calling in Inheritance

This is an exciting thing in the inheritance because we use constructors to make an object of a class. So, what if we only make the object of a subclass or a derived class? What will happen then? Will the constructor of the superclass be called?

So, the easiest way of understanding it is just by doing it 😛

Base.Java

public class Base {
	
	public Base(){
		System.out.println("Base class constructor");
	}

	public void display() {
		System.out.println("display method in base class");
	}

}

Derived.java

public class Derived extends Base {

	public Derived() {
		System.out.println("Derived class constructor");
	}
}

Now let’s create an object of the Derived class and see what happens

public class Codekru {

	public static void main(String[] args) {
		Derived derived = new Derived();
		derived.display();
	}
}

Now, run the above class, and you will get your answer.

Base class constructor
Derived class constructor
display method in base class

Yes, the Base class’s constructor will be called first, and then only the Derived class’s constructor will be called, but why is that?

What happens is that Java inserts the super() function in the constructor of the Derived class by default on the first line. So Derived class constructor will look like this-

public Derived() {
	super();
	System.out.println("Derived class constructor");
}

What super() does is that it calls the default constructor of the Base class, and hence the constructor of the Base class will execute first. If you are uncomfortable with the super keyword yet, don’t worry; we will discuss it in great detail later in our articles.

Types of inheritance in Java

Based on classes, there can be three types of inheritance in Java –

  • Single inheritance
  • Multilevel inheritance
  • Hierarchical inheritance

There are two other types of inheritance in java – Multiple and Hybrid Inheritance, but they are supported only through the interfaces, which we will learn about later in the posts.

Single Inheritance

When a class inherits the properties of another class, this is known as single inheritance.

Single Inheritance in java
class Alpha{
	
	public void alphaMethod() {
		System.out.println("Method in alpha class");
	}
	
}

class Beta extends Alpha{
	
	public void betaMethod() {
		System.out.println("Method in beta class");
	}
	
}

public class Codekru {

	public static void main(String[] args) {
		Beta beta = new Beta();
		beta.alphaMethod();
		beta.betaMethod();
	}
}

Output –

Method in alpha class
Method in beta class
Multilevel Inheritance

When derived class also acts as a base class for another class. It is known as Multilevel inheritance. In the figure below, you can see that the Beta class inherits from the Alpha class, and then the Gamma class further inherits from the Beta class.

Multilevel Inheritance in java

So, the Gamma class will inherit the properties of both Alpha and Beta classes, as shown in the below example.

class Alpha {

	public void alphaMethod() {
		System.out.println("Method in alpha class");
	}

}

class Beta extends Alpha {

	public void betaMethod() {
		System.out.println("Method in beta class");
	}

}

class Gamma extends Beta {

	public void gammaMethod() {
		System.out.println("Method in gamma class");
	}
}

public class Codekru {

	public static void main(String[] args) {
		Gamma gamma = new Gamma();
		gamma.alphaMethod();
		gamma.betaMethod();
		gamma.gammaMethod();

	}
}

Output –

Method in alpha class
Method in beta class
Method in gamma class
 Hierarchical Inheritance

When one class is the base class for more than one class, it is known as Hierarchical inheritance.

Hierarchical inheritance in java

In this case, the Beta class will inherit properties from the Alpha class, and the Gamma class will also inherit properties from the Alpha class.

class Alpha {

	public void alphaMethod() {
		System.out.println("Method in alpha class");
	}

}

class Beta extends Alpha {

	public void betaMethod() {
		System.out.println("Method in beta class");
	}

}

class Gamma extends Alpha {

	public void gammaMethod() {
		System.out.println("Method in gamma class");
	}
}

public class Codekru {

	public static void main(String[] args) {
		Gamma gamma = new Gamma();
		gamma.alphaMethod();
		gamma.gammaMethod();

	}
}

Output –

Method in alpha class
Method in gamma class

There are two more types of inheritance in Java – Multiple and Hybrid inheritance, but they cannot be achieved using class inheritance; instead, we have to use the interface implementation for those. Below is the diagrammatical representation of both inheritances, but we will discuss these in later posts.

Multiple inheritance in java
Hybrid inheritance in java

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Related Articles –
Liked the article? Share this on

2 thoughts on “Inheritance in Java ( Is-A relationship)”

Leave a Comment

Your email address will not be published.