The word ‘Polymorphism in Java’ literally means

‘a state of having many shapes’ or ‘the capacity to take on different forms’.

An important example of polymorphism is how a parent class refers to a child class object.  In fact, any object that satisfies more than one IS-A relationship is polymorphic in nature.

Refer my previous article on Inheritance to know about IS-A relationship using below link,

Types of Polymorphism

  • Compile time polymorphism or static polymorphism)
  • Runtime polymorphism or dynamic polymorphism).

Method overloading is an example of static polymorphism

Method overriding is an example of dynamic polymorphism.

Imagine below diagram for understanding Polymorphism in Java.

Bank IS-A Object

SBI IS-A Bank

HDFC IS-A Bank

AXIS IS-A Bank and so on for IOB, ICICI, CITI.

Static Polymorphism or Compile-time Polymorphism

Example:

public class Bank {

	// general rate of interest
	public float getRateOfInterest() {
		return 5.0f;
	}
	
	// input only customerType
	public float getRateOfInterest(String customerType) {
		if(customerType.equals("PERSONAL")) {
			return 6.0f;
		}else if(customerType.equals("COMMERCIAL")) {
			return 8.0f;
		}else {
			return 9.0f;
		}
	}
	
	
	// inputs as customerType and loanType
	public float getRateOfInterest(String customerType, String loanType) {
		if(customerType.equals("PERSONAL") && loanType.equals("HL")) {
			return 6.0f;
		}else if(customerType.equals("PERSONAL") && loanType.equals("PL")) {
			return 7.0f;
		}else if(customerType.equals("COMMERCIAL") && loanType.equals("HL")) {
			return 8.0f;
		}else if(customerType.equals("COMMERCIAL") && loanType.equals("PL")) {
			return 8.5f;
		}else {
			return 9.0f;
		}
	}
}

During code compilation, the compiler verifies that all invocations of the getRateOfInterest() method correspond to at least one of the three methods defined above in Bank Class.

Dynamic polymorphism or Runtime polymorphism

With dynamic polymorphism, the Java Virtual Machine (JVM) handles the detection of the appropriate method to execute when a subclass is assigned to its parent form. This is necessary because the subclass may override some or all of the methods defined in the parent class. Find the example below.

public class SBI extends Bank{

	@Override
	public float getRateOfInterest() {
		// TODO Auto-generated method stub
		return 6.5f;
	}
}
public class Axis extends Bank {

	@Override
	public float getRateOfInterest() {
		// TODO Auto-generated method stub
		return 7.0f;
	}
}
public class HDFC extends Bank {

	@Override
	public float getRateOfInterest() {
		// TODO Auto-generated method stub
		return 8.0f;
	}
}
public class TestROI {

	public static void main(String[] args) {
		Bank bank1 = new SBI();
		Bank bank2 = new HDFC();
		Bank bank3 = new Axis();
		
		System.out.println(bank1.getRateOfInterest());
		System.out.println(bank2.getRateOfInterest());
		System.out.println(bank3.getRateOfInterest());
	}
}

Output

6.5
8.0
7.0

When you look at the TestROI class, the SBI, HDFC, Axis object are assigned to bank object variable. But whenever getRateOfInterest() method is invoked, JVM determines actually from which class the method is invoked and returns the appropriate value.

Using Polymorphism with Collections

Every class in Java extends class Object

Class Object is the mother of all classes. It’s the superclass of everything. You can take advantage of polymorphism to create a class with methods that take and return your polymorphic type. Without a common superclass for everything in Java. there’d be no way for the developers of Java to create classes with methods that could take your custom types.

Any class that doesn’t explicitly extend another class, implicitly extends Object class

Imagine below example to understand polymorphism in Java with collections

// Creating ArrayList of Banks and adding banks as required
ArrayList<Bank> banksList = new ArrayList<Bank>();
banksList.add(new HDFC());
banksList.add(new SBI());

In case if we have a requirement where some commercials shops also lend us loan and we need to consider their rate of interest. So we need to add those shops also in banks list which is not possible with above ArrayList as it is if type <Bank>. So we are going to modify the example as below,

ArrayList<Object> objectList = new ArrayList<Object>();
objectList.add(new HDFC());
objectList.add(new SBI());
objectList.add(new Amazon());
objectList.add(new Flipkart());

In above modified code, we are declaring ArrayList as type <Object> and hence we can accomodate any object type into the ArrayList. But what happens when you try to get the Bank object assigned to Object reference? It will throw a compile error for below line of code,

Bank bank1 = objectList.get(0);

Everything comes out of an ArrayList<Object> as a reference of type Object, regardless of what the actual object is, or what the reference type was when you added the object to the list.

Objects come out of an ArrayList acting like they’re generic instances of class Object. The Compiler cannot assume the object that comes out is of any type other than Object.

Conclusion

Hence we saw what is polymorphism, types of polymorphism with example and how to use polymorphism with collections.

Please follow and like us:
error