Computing Magazine

Java Inheritance and Polymorphism

Posted on the 12 October 2016 by Skiabox @skiabox
Java Inheritance and Polymorphism

[vc_row][vc_column][vc_column_text]This is an article about basic inheritance and polymorphism in Java Programming Language.
Let's start by creating a class hierarchy based on animals in a zoo.
First we create the Animal base class.
The central idea is that we have a group of animals living inside a zoo.
This group of animals consists of Lions, Tigers, Cats, Hippos, Wolfs and Dogs.
They can do certain things like sleeping, roaming, making noise or eating.
Along with the things that they can do they also carry specific characteristics like the kind of food they prefer (meat or grass), how much hungry they are at a specific moment and their location in the zoo.
Their image too, is a key characteristic of every animal.

To formulate in a java class all these common characteristics we decide to create an abstract class called Animal.
The following image is a diagram of the Animal abstract class :

And here you can see the code of the Animal class:

View the code on Gist.

Animal class is very general so we declare this class as abstract.
The fact that the animal class is abstract does not prevent us from writing some methods that can be used in another class or preferably to a number of classes that are positioned below the Animal class in the inheritance tree.
Notice the two highlighted lines (77 and 78).
These abstract methods must be implemented (overridden) by some class below the animal class at the hierarchy tree.

At this point we can see two more great categories of animals that introduces some more common behaviour.
For example Felines (Lions, Tigers, Cats) tend to avoid others of their own kind when they move and Canines (Wolfs, Dogs) tend to move in packs.
We can use this fact to create a couple more of abstract classes.
Let's see the application diagram and where is the position of these two new abstract classes:

And here is the code of the two abstract classes :

[tabby title="Feline class"]

View the code on Gist.

[tabby title="Canine class"]

View the code on Gist.

[tabbyending]

As you can see we override here the implementation we had provided in the parent Animal class.
Let's see how this theory behaves in action with real objects.
I have created a small application for this article and here is the code :

View the code on Gist.

If you check this code, you'll see that at line 30 we create a new Hippo object.
You can see the Hippo class diagram and code right below :

View the code on Gist.

As we see here the Hippo has its own implementations of makeNoise() and eat() methods, and thus it fulfils the obligation to provide concrete implementations of abstract methods that exist above him at the class hierarchy tree.

Now we can run a small application that I have been created for this article.
Here is the application code :

View the code on Gist.

When we run this code we are getting the following result at the console window :

[plain]Inheritance in action
------------------
The wolf howls
The Canines move in packs
, and now that it is a wolf it is very dangerous for its prey
The wolf is eating MEAT
The animal sleeps

Polymorphism in action
------------------
The dog is eating GRASS
The Canines move in packs
The cat eats GRASS
The Felines avoid each other
The wolf is eating MEAT
The Canines move in packs
, and now that it is a wolf it is very dangerous for its prey
The hippo is eating GRASS
The animal is roaming
The lion is eating MEAT
The Felines avoid each other

Polymorphic arguments
------------------
The vet is giving a shot to Wolf
The wolf howls

Process finished with exit code 0[/plain]

At line 18 the Hippo is using its own method but at the next line (19) the Hippo is using the generic roaming method from his parent Animal abstract class.

As you can see in my small program we can declare an object to be of any type above its class in the inheritance tree.
So with this array of Animal type we are getting very flexible because we can assign to the Animal variable any concrete object instance at runtime.

Now let's turn our attention at the wolf object.
The rule here is that when you are calling an object method, the most specific version of the method is get executed.
You can see that in App2.java, lines 14-17.
Another thing to point here is that wolf's own roam method is using its parent (Canine class) roam method by using the super keyword.
Check how this work by looking at the Wolf class diagram and code :

View the code on Gist.

Let's see now the complete inheritance tree and the rest of the various class code.

[tabby title="Cat class"]

View the code on Gist.

[tabby title="Dog class"]

View the code on Gist.

[tabby title="Lion class"]

View the code on Gist.

[tabby title="Tiger class"]

View the code on Gist.

[tabbyending]

Finally we can use polymorphism to declare method arguments as a general type and then pass as an argument any concrete object that is of this type.
Check for example the Vet class.

View the code on Gist.

We can send in any animal we want and as we saw at the console window posted before every animal reacts with a different noise!
That's all for today.
See you at my next article when we will talk about interfaces.

PS : A link with the whole project code - > Java inheritance and polymorphism Github Project[/vc_column_text][/vc_column][/vc_row]


Back to Featured Articles on Logo Paperblog