Java Class Inheritance | Types of Inheritance in Java

Java Class Inheritance is one of the crucial lessons to learn since there are many related concepts. We will see all the types of Inheritance in Java that are there, and wrestle with some examples to understand everything about them. Rest of the stuff we are going to learn as we code. Alright, so we on the same page? Good!

What is Inheritance?

When you see the word inheritance what’s the first thing that comes to your mind? That somebody is going to get lucky by inheriting a fortune, right? I know because that’s how my evil mind works too. Trust me, I can relate.

inheritance meme

Crazy as it might sound, inheritance is more or less based upon the aforementioned theory. Here we are basically inheriting properties of a class. Properties could entail methods and fields, in short everything a class has. The reason we choose Java Class Inheritance is because it helps us to save a lot of time and effort since we don’t have to write all those methods and fields all over again. It also helps to put things in a more systematized hierarchical order.

  • The class which inherits the properties is known as subclass. People often call it a derived class or child class as well.
  • The class whose properties get inherited is known as superclass. It also goes by the names base class and parent class.

The best part of the above setup is that a subclass can still go on to add its own additional variables and fields. It could further help you to identify which one’s a subclass or a superclass. Brace yourself proper examples to understand Java Class Inheritance are inbound.

Java Class Inheritance: Superclass and Subclass Setup

Is there a doubt lingering? I will try to explain the concept of superclass and subclass using the following dumbed down example:

image to depict single level java class inheritance

Imagine you are sitting in a restaurant. You have just ordered a potato sandwich. The cook is busy prepping it up, when suddenly you change your mind and wish to go for an egg potato sandwich instead. Of course, you are going to get a scornful sneer from the waiter, don’t try that stunt.

When the waiter would tell the cook about your new choice now, he would realize that his ‘potato sandwich’ hasn’t become a complete waste yet, that there is still a window of opportunity to get things sorted out. Reason being, you asked for an egg potato sandwich which is simply an upgrade from a potato sandwich. So he can simply prepare an egg and ‘add’ it to the existing potato sandwich. So all the properties of the superclass ‘potato sandwich’ were thoroughly used to get you a subclass ‘egg potato sandwich’ where egg is nothing but an additional property.

Got it?

Now when your egg potato sandwich is just about to be served, and you call for a pizza instead, then I am sure you will be thrown out of the restaurant for being a crazy ass. Also, in tech terms, pizza would be an entirely different and unrelated class. None of the properties of potato sandwich would be useful then and you would be forced to create a new class.

All this food talk has made me hungry. *tummy grumbles*

Syntax to Use ‘Extends’

So how do we use inheritance in Java? Is there a syntax? Yes, there is. We make use of the ‘extends’ keyword. We use it with the subclass then follow it up with the superclass. So the syntax would be like:

class Subclass extends Superclass {}

So if you take the aforementioned example into account, where PotatoSandwich and EggPotatoSandwich are super and subclasses, then to make EggPotatoSandwich a subclass of the PotatoSandwich our code might appear something like this:

class PotatoSandwich {

}

class EggPotatoSandwich extends PotatoSandwich {

}

With that, I think it is time to delve into a proper Java Class Inheritance example.

Here’s a class called PotatoSandwich with its own set of data members:

image of a class with its data members

Now we will create a subclass called EggPotatoSandwich and make use of ‘extends’ to get all the data members under the aegis of our new class. I will just make it a new file altogether just so we learn that it is alright to create separate files for different classes. Also to introduce you to the benefit of using access identifiers which I am going to explain later.

Creating EggPotatoSandwich class in a separate file

Notice how we have used the extends keyword to inherit all that belongs to PotatoSandwich in this very subclass? We have also created a display() method which is an additional method that the superclass isn’t aware of. Trying to display ‘potato’ which was actually a String variable declared in the superclass PotatoSandwich, goes on to show that there will be no errors if we talk about the variable here, since all the data members of superclass are inherited now.

I will go ahead and use a main method to create an object of this class, and try to show you that calling the display method does not give us any error:

In any case you don’t understand the code, feel free to go back on how to create objects and classes and learn the basics first.

image of EggPotatoSandwich subclass

Now simply running the above program we will get the result as:

sweet

What if I try to create an object of PotatoSandwich, and try to call its very own method eat? Doing so will make our code look like:

image for calling methods of subclass and superclass by creating objects

Click on the green arrow button to execute this program once again.

You will get a result that calls both methods one by one:

result for calling a superclass and subclass method

Types of Inheritance

Now, this takes us to Round 2. What are the types of Inheritance? Can this ‘extends’ keyword go on and on? Can there be numerous levels of inheritance?

There are different types of Inheritance that exist. But only four are supported by Java. So, the four Java Class Inheritance types are:

  1. Single
  2. Multi-level
  3. Hierarchical
  4. Hybrid or Virtual

The example that we had seen above was actually a type of single level inheritance. If there’s another level to it, say an Egg Ham Potato Sandwich that inherits the quality of Egg Potato Sandwich as well, then it becomes a multi-level inheritance.

Multi-Level Inheritance

To put it in a diagram:

image depicting multi-level inheritance in java

So if we have another class that is extending the EggPotatoSandwich in the above example we can say that we have achieved a multi-level inheritance. The levels, of course, can keep on increasing as long as there are properties to inherit, so it goes on and on and on.

Taking a look at how things might appear if we add another level to our existing code. Let’s create a third class file titled EggHamPotatoSandwich:

code to depict multi-level inheritance

See, how I have created a third instance and then used it to display a method of the previous class. So the display method has been used twice, you are going to get a result that is on the following lines:

output to display multi-level inheritance results

Hierarchical Inheritance

It is something that allows the creation of more than one derived classes from a single base.

real life example of superclass and subclass

The above diagram will help you understand it better. Here we have another subclass, apart from the first Egg Potato Sandwich, that is trying to inherit the superclass.

If we try to understand things with the help of our code:

image for HamPotatoSandwich Class in Java

There’s a class at the same level as that of EggPotatoSandwich which is a subclass of PotatoSandwich, the ultimate superclass.

If you run the above program you will get the result as:

sweet

once again. Go ahead and try!

Hybrid Inheritance

Hybrid inheritance is simply a combination of more than one inheritance. For example, it could be making use of hierarchical and multilevel both. In an attempt to create the hierarchical I had accidentally created a hybrid inheritance as well.

So, we have now four class files namely PotatoSandwich, EggPotatoSandwich, HamPotatoSandwich and EggHamPotatoSandwich which would serve as a perfect example of hybrid inheritance.

diagram for hierarchical inheritance in java

I am not going to write the code for it again, since we have already covered it involuntarily.

Multiple Inheritance

You might say why isn’t there a scenario wherein there is more than one Superclass for one single subclass? Well, there used to be in programming languages like C++, but Java decided to keep things clean and healthy.

But it doesn’t mean that Java failed to understand the concept of multiple inheritance. Java went on to create a better way to tackle multiple inheritance. It just goes by a different name.

Yes in order to tackle that situation we have the concept of Interfaces in Java.

Don’t worry I am not going to start off immediately after finishing Java Class Inheritance hereby. That’s the next chapter I intend to cover.

Till then go see a movie or something. You deserve it!

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

3 Responses

  1. May 16, 2017

    […] An abstract class cannot be instantiated. The only option you have is to use it as a superclass. Hope you remember the inheritance lesson. […]

  2. May 30, 2017

    […] while working with inheritance access control follows certain […]

  3. June 4, 2017

    […] Polymorphism is a major OOPS concept that you should know about if you wish to be a Java pro. It is closely associated with Inheritance. […]

Leave a Reply