Learn Polymorphism In Java

Sharing is healthy !!

Here we are going to learn polymorphism in java. In this post we will discuss what is polymorphism in java and how we can use polymorphism in java by an example. So let’s get into it,

Learn Polymorphism In Java: what is polymorphism?

Polymorphism is the ability of an object to have different forms.

So polymorphism literally means having different forms and whenever you define class object and its property of having many forms is called as polymorphism.

Let’s see an example on how you can define an object which follows polymorphism,

I have four classes defined, one is called the Company class which will be the base class. This class contains one method and this method gives the share price, that is, see below

// Base Class

public class Company {
 
int getSharePrice() {

return 0;

}
}

and the method getSharePrice() returns 0 as share price.

Next i have defined Company_A class, Company_B class and Company_C class. See below,

// Derived Class

public class Company_A extends Company{
 
int getSharePrice() {

return 5;

}

}
// Derived Class

public class Company_B extends Company{
 
int getSharePrice() {

return 6;

}

}
// Derived Class

public class Company_C extends Company{
 
int getSharePrice() {

return 10;

}

}

Also Read – Java Example To Print All Alphabets

Above three classes extends the base class Company and have defined method getshareprice() in each of the above three classes including base class.

Now there is a property in java called polymorphism by which you can define the reference to point to a base class to any object of the derived class.

That is when a reference object or reference of parent class points to the object of the subclass it is called upcasting and this is the basic extract of polymorphism.

public class Polymorphism_Example {

public static void main(String[] args) {
 
Company a = new Company_A();
Company b = new Company_B();
Company c = new Company_C();
 
System.out.println(a.getSharePrice());
System.out.println(b.getSharePrice());
System.out.println(c.getSharePrice());

}

}

Output:

5
6
10

As you can see above i have defined the reference of company class as ‘a’. This will point to the object of Company_A which is a derived class or child class.

This is what we call polymorphism. So here the object of Company class is having the form of Company_A.

So the reference of Company class is pointing to the object of Company_A class. In the same way we can define different objects as shown in the above example.

The interesting thing here is all references are pointing to different objects but all objects are from Company class. So this is how polymorphism work in java.


Learn Polymorphism In Java: types of polymorphism

Polymorphism in java has two types

  • Compile time polymorphism
  • Runtime polymorphism

compile time polymorphism:

Compile time polymorphism is also called as static binding.

It is an method overloading technique. In this, class can have more than one method.

The methods have same name but with different number of arguments. It can be implemented in a single class.

Compiler is able to figure out the method call at compile time that’s the reason it is known as compile time polymorphism. Let’s see an example,

Read Also – Matrix Transpose In Java

public class StaticBinding_Example {

public static void main(String[] args) {
 
Addition ab = new Addition();
ab.add(10, 4); // method call
ab.add(6,2,4); // method call

}
}

class Addition
{
void add(int a,int b)
{
System.out.println("Adding two integers: " + (a + b));
}
void add(int m,int n,int o)
{
System.out.println("Adding three integers: " + (m + n + o));
}
}

Output:

Learn Polymorphism In Java

runtime polymorphism

Runtime polymorphism is essentially referred as method overriding. It is also called dynamic binding. Let’s get into some details using an example,

public class vehicle{
 
public void driving()
{
System.out.println("Mr.X is driving vehicle..");
}

}

class Car extends vehicle
{
@Override
public void driving()
{
System.out.println("Mr.X is driving car..");
}
}

class Lorry extends vehicle
{
@Override
public void driving()
{
System.out.println("Mr.X is driving lorry..");
}
 
public void load()
{
System.out.println("Mr.X is loading lorry..");
}
}
public class Runtime_Example {

public static void main(String[] args) {
 
vehicle v = new vehicle();
v.driving();
 
v = new Car();
v.driving();
 
v = new Lorry();
v.driving();
 
Lorry lr = new Lorry();
lr.load();

}

}

Output:

Learn Polymorphism In Java

Here i have defined vehicle class and Runtime_Example which has main method.

In the above object declaration and instantiation there are two parts: one, type of the reference and another, type of the object created. In the above example,

Vehicle v = new Car();

the reference type is Vehicle and the object created is of type Car. Above assignment is only possible when object created type is a subclass of the reference type.

Each object reference can be used to invoke methods and the methods which can be invoked is decided based on the reference type and this is decided during the compile time.

But the implementation to be invoked is decided based on the type of the object created. In the above example statement

v.driving();

compiles because the drive() method is part of the vehicle class and gives

"Mr.X is driving car.."

as the output because the method is overridden by the Car class.

Similarly

lr.load();

gives compile time error because load() is not part of the vehicle class, but is defined only in the Lorry class.

But the lr.load() compiles because the reference type is Lorry class and the compiler can resolve the load() method.

The selection of the method’s implementation to execute happens at the runtime, that is, which implementation of the method to be executed, that is, the parent class or child class is decided at the runtime and this is what leads to runtime polymorphism.

Sharing is healthy !!

Leave a Reply

Your email address will not be published.