Your guide for Inheritance & Polymorphism concepts!

Dasari Swaroop Kumar
5 min readNov 8, 2020

This is my take on explaining Inheritance and Polymorphism concepts to everyone in the simplest way possible with real-time examples.

Inheritance:

Let’s take an example to understand what inheritance actually is, Everyone knows that they inherit their parent's property and your’s to your children and this process continues, right?

what do you inherit from your parents? their properties like land, gold, wealth that they have created for us, etc., Some parents, on the other hand, want their children to earn their own money and live out of it, they teach their children the value of money. Assume that their property doesn’t go to their children and it goes to charity.

Now let me connect the above story with the java inheritance concept.

Assume that I have a class Parent with 2 fields, ‘land’ and ‘wealth’ and class Child is extending the class Parent. From the above example, assume that this is the first type of parent. So, Child will inherit the properties of the Parent i.e., Child will have access to ‘gold’ and ‘wealth’ properties of Parent class.

The second type of parent, on other hand, doesn’t give access to the property he has to the Child class. This can be achieved by using the ‘private’ modifier in java. using this modifier prevents the Child class to inherit those fields from the Parent class.

For the rest of the part, let’s take the first type of parent as Parent class.

Assume that the Parent class has two methods, m1() and m2(). Hence Child class also has access to those methods. Even Child class can have it’s own methods and fields too. One of the main advantages of this concept is that Child class need not have m1() and m2() methods in it again, which adds up to the feature of re-usability of code. (eliminates redundancy of code)

Code for the above concept will look something like this,

package com.example.helloworld;

class Parent{
String land;
String wealth;
//Parent constructor
Parent(String land,String wealth)
{
this.land=land;
this.wealth=wealth;
}
//adding methods
public void m1()
{
System.out.println("Method m1() inside Parent");
}
public void m2()
{
System.out.println("Method m2() inside Parent");
}
}
class Child extends Parent {
String childWealth;
Child(String land,String wealth,String childWealth)
{
super(land,wealth);
this.childWealth=childWealth;
}
//can have it's own methods!

}

public class HelloWorld{

public static void main(String[] args) {
Child c=new Child("20Acres","20cr","20lk");
System.out.println(c.land);
System.out.println(c.wealth);
System.out.println(c.childWealth);
c.m1();
c.m2();
}
}

Multiple Inheritance:

Let me explain why java doesn’t support multiple inheritance wrt classes? using the same theory.

Assume that you extend your Uncle class along with your Parent class. Your Java compiler will face ambiguity problems when you try to access land and wealth using child reference since these two properties are present in both classes, the compiler doesn’t know which class’s field to choose either Uncle or Parent class’s one. So, SUN microsystems analyzed this possibility and disabled it in the first place.

Before going to Polymorphism, let’s have clarity on overloading, overriding concepts.

Overloading:

Two methods are said to be overloaded if both methods have the same name but different number of parameters or different datatypes or combination of both.

Here’s a simple example on how it works,

class Test{
//instance and static variables declaration
public int add(int a,int b){ //perform addition of 2 numbers
int sum=a+b;
return sum;
}
public int add(int a,int b,int c){

//perform addition of 3 numbers
int sum=a+b+c;
return sum;
}
public static void main(String[] args)
{
Test t=new Test();
System.out.println("Sum of two Numbers: "+t.add(10,20));
System.out.println("Sum of three Numbers: "+t.add(10,20,30);
}
}

Overriding:

Two methods are said to be overridden if both methods have the same method signature but have a different return type.

Let’s take the same example of analogy that we have taken for illustrating the inheritance concept.

Assume that in Parent class there is a method called spendLimitPerDay(), which fixes the amount of money spent by you per day. Assume Child class doesn’t like the implementation of that Parent class method(Meaning that Child wants to spend more money than Parent assigned to it).

The above problem can be overcome using the concept of Overriding.

package com.example.helloworld;

class Parent{

//adding methods
public void spendLimitPerDay()
{
System.out.println("Limit set to 400/- per day");
}

}
class Child extends Parent {

//Overriding in action!
public void spendLimitPerDay()
{
System.out.println("Limit set to 800/- per day");
}
}
public class HelloWorld{

public static void main(String[] args) {
Parent p=new Parent();
p.spendLimitPerDay();
Child c=new Child();
c.spendLimitPerDay();
}
}

Polymorphism:

Polymorphism means the ability to exist in many forms.

Let’s take an analogy of me being a son to my parents and an employee to a company. That is me in two different forms!

Being a son, I must do what I can do in that role and the same goes for an employee of a company, I must fulfill what’s needed at my job.

Assume that a situation demanded you to do yourOfficeWork at your home or yourHomeWork at your office. It’s a valid situation, right? So, what you do is you will finish yourHomeWork at your office itself(your runtime is in the office, but you finish yourHomeWork) and vice versa in the other situation.

Let me put all the above theory into code,

package com.example.helloworld;

class You{
private String work;
You(String work){
this.work=work;
}
public String getWork(){
return work;
}
}
class Home extends You{

Home(String work) {
super(work);
}


//adding methods
public void yourHomeWork()
{
System.out.println("Executing yourHomeWork() in Home class and Work: "+this.getWork());
}

}
class Office extends You {

Office(String work)
{
super(work);
}

//adding methods
public void yourOfficeWork()
{
System.out.println("Executing yourOfficeWork() in Office class");
}
}
public class HelloWorld{

public static void main(String[] args) {

//home demanding you for home related work
Home home=new Home("Call Plumber");
home.yourHomeWork();

//office demanding you for office work
Office office=new Office("Submit given work by tomorrow");
office.yourOfficeWork();

//home demanding work during office hours
You theHomeWork=new Office("Come home early today");
System.out.println(theHomeWork.getWork());

//office demanding work, on my day off at home
You theOfficeWork=new Home("Attend client meeting at 10:00AM");
System.out.println(theOfficeWork.getWork());


}
}

What was the advantage of holding the runtime object inside Parent reference?

The answer would be, based on the runtime necessity it’s possible for adaptation of behaviour of the methods.

Another classic example to illustrate polymorphism would be wrt Collections.

It may seem like I’m hard coding objects in polymorphism. But when it comes to collections, it’s a powerful concept. Assume we have a List of Objects (which can contain any object in it). You will never know which object is going to show up during runtime. So, in those cases, these polymorphism principles will play a vital role and make our lives easier.

let me prove it with an example.

package com.example.helloworld;

import java.util.ArrayList;
import java.util.List;

public class HelloWorld{

public static void main(String[] args) {

List theList=new ArrayList();
theList.add("String Object");
theList.add(new StringBuffer("String Buffer Object"));
theList.add(10);
theList.add(10.4f);
theList.add(10.9);
System.out.println(theList);

//check the type of variable I used to hold each object inside theList
for(Object checkThis:theList){
if(checkThis instanceof StringBuffer)
{
System.out.println("Got hold on StringBuffer Object.");
break;
}
}

}
}

That’s all for now on the concepts of Inheritance and Polymorphism. Explore more, learn more about these concepts on official java docs.

--

--