Inheritance in OOP Java

Image Credit - Pexels

Going back to the Beginning 🙂

This article covers one of the most powerful concepts in Object Oriented Programming, Inheritance. Guys, what would come to your mind when you hear the term, Inheritance?? Nothing… or some kind of Charles Darwin’s evolution related stuff 🙂

You know, we inherit the physical and, probably, the mental features of our parents and parents inherit them from their grandparents and grandparents inherit them from their grand-grandparents……blah blah…It goes back to the beginning of the life on earth.

Ah, another thing. It’s true we receive the features from our parents, but, we may also develop our own talents that they don’t have. Right? That’s how evolution has produced this beautiful diversity among us.

So, while inheriting the old features, we can give new features to our next generation…

There’s one final thing as well. Sometimes, we may NOT receive some abilities that our parents have.

This is the concept of inheritance. Now, programming also has this inheritance. Let’s see…

We can create a new Class that can produce objects. We can have variables, methods, constructors in those classes.

An Example

Suppose, I want to make a game and for the environment of the game, I need a Tree object. For that, I need to make a Class called Tree.

Now this Tree Class could be very complex. It may produce a 3D model of a Tree which will appear real in the game. Forget it for now.

Let’s make everything simple here… OK. Now let me add some basic features of a tree to our Class Tree. What a tree object contains? Let’s add two member variables for the height of the tree, number of branches and two method to draw the tree on the screen. We can add more features to the class Tree as necessary..

Now suppose I want to customize this tree. I need an orange tree instead of just a tree. So how do I do that? Starting a new class Orange tree from scratch? NO!!

We already have a Tree class that has common properties for all trees. If we are going to create an Orange tree, we have to rewrite that part somehow. But luckily, we have inheritance to avoid this repetition.

With the “extends” keyword, we are making the class OrangeTree as a child class of the parent Class Tree. Child inherit all the features of the parent.

Similarly, the OrangeTree doesn’t need to redefine the member variables height and branchesNo. They are automatically generated when we extend OrangeTree to Tree. Similarly, the methods printTreeDetails() and draw() of Tree are available in OrangeTree now.

Now I want to add fruits to a OrangeTree object. For that, I create a new method addFruits() in the class OrangeTree. Also, I create another variable hasLeaves to store a boolean value to mention whether the tree has leaves or not. Perhaps, leaves might have fallen in winter, you know 🙂

This is the basic concept of inheritance.

Another Example of Inheritance

Now, if we take another example, say, vehicles, I can first create the class Vehicle.

I can include the basic things that are common to all vehicles such as,

  • registration number,
  • number of wheels,
  • number of passengers that the vehicle can carry

in member variables

and some methods like

  • turn lights on,
  • Increase speed,
  • decrease speed etc..

Then I can create child classes such as Car, MotorCycle, Bus..etc which include all features of the class Vehicle but having some extra features as well.

For example, the Car class may have the member variable, noOfDoors to tell how many doors are in that car which is not required in the MotorCycle.

Now, the interesting fact is, I can again create classes that are child class of the second generation. For instance, take the second generation Class Car.

Now I want a BMW car in my program. So I can create a Class BMW which inherit all features from the class Car. But BMW should have extra features that a usual car might not have. Similarly, other car brands may have different features than BMW. So, if I create a Benz or Toyota car Class, it may also inherit the Car class.

You know, every Object that we use in Java is inherited from a very basic, fundamental class called “Object”. It is the class which is at the top.

Back to the Car… How do you create a Car Object?

Here, in the declaration statement, we have declared the Object type as Car.

But there’s very VERY important point. You can declare it as a type of parent Class!!

This could be confusing. So understand the concept thoroughly.

There’s a subtle point. If you do this, you cannot use the extra features of the Car Class. Only the basic  features of Vehicle is available in the variable basicCar1 since it is a Vehicle type, not a Car type.

So, if you want all the features of the Car back, you can type cast the variable basicCar1 to type Car.

So, the conclusion is you CAN declare the type of a child Class object using the parent Class.

But the opposite of this is FALSE! That is, you CAN’T create parent class object in a variable declared to be a child class type.


OK guys. I think this is enough for the time. The previous note, parent class declaration for the child class is really important to understand well. This is frequently used in programming. So, I strongly recommend you to understand this clearly and proceed forward. BYE! 🙂