Learn polymorphism in java

Hey guys!!! Welcome to flower brackets blog. Here we are going to learn polymorphism in java.

Polymorphism is the ability of an object to have different forms. Polymorphism literally means having different forms and whenever you define class object and its property of having many forms is called as polymorphism.

Also read – learn inheritance in java

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,

// Base Class
public class Company
{ 
   int getSharePrice()
   {
      return 0;
   }
}

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

Next I have defined class CompanyA, class CompanyB and class CompanyC,

// Derived Class
public class CompanyA extends Company
{ 
   int getSharePrice()
   {
      return 5;
   }
}

// Derived Class
public class CompanyB extends Company
{ 
   int getSharePrice()
   {
      return 6;
   }
}

// Derived Class
public class CompanyC extends Company
{ 
   int getSharePrice()
   {
      return 10;
   }
}

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 PolymorphismExample
{
   public static void main(String[] args)
   {
      Company a = new CompanyA();
      Company b = new CompanyB();
      Company c = new CompanyC();
 
      System.out.println(a.getSharePrice());
      System.out.println(b.getSharePrice());
      System.out.println(c.getSharePrice());
   }
}

Output:

5
6
10

As you can see above we have defined the reference of Company class as ‘a’. This will point to the object of CompanyA 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 CompanyA.

So the reference of Company class is pointing to the object of CompanyA 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.


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 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,

public class StaticBindingExample
{
   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 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 RuntimeExample
{
   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 class vehicle and class RuntimeExample has main method.

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

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 class Vehicle, but is defined only in the class Lorry.

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

The selection of method to execute happens at runtime, that is, which method to execute, that is, parent class or child class is decided at runtime and this leads to runtime polymorphism.

Related Posts