When to use Polymorphism?

We have read polymorphism in previous topics. We have seen many things about static polymorphism and dynamic polymorphism. But the most important part to know when to use polymorphism real time example? In this article, we will discuss when to use static polymorphism and dynamic polymorphism. If you are new here and You have not read the static polymorphism and dynamic polymorphism yet. Then you should read static polymorphism and dynamic polymorphism moving further before.

1. When to use static polymorphism?
2. When to use dynamic polymorphism?

When to use static polymorphism real time example?

First scenario 1:  We can overload a method when we want to perform different operations on the base of parameters. Let’s take the example of a String class.

public int indexOf(int ch)
public int indexOf(int ch, int fromIndex)

public indexOf(int characterName)This method accepts only one parameter. It returns the index value of the first occurrence of the specified character You can read this method in detail.

indexOf(int characterName, int fromIndex)This method accepts two parameters. One parameter is the name of a character and another the index to start the search from. It returns the index value of the first occurrence of the specified character. You can read this method in detail.

Now you have understood the reason for method overloading. Here both methods perform different functionality by the different numbers of parameters.

class ExampleOfIndexOf
{ 
       public static void main(String args[])
       { 
	String name = "JAVA WINGS"; 
	int index = name.indexOf('A');
        System.out.println("The index of first occurrence of given character is = "+index);
	
	int index1 = name.indexOf('A', 2);
	System.out.println("The index of character after the specified index for search = 
	"+index1); 
	}
}

Output: Output: The index of first occurrence of given character is = 1
The index of character after the specified index for search = 3

First scenario 2: Suppose we are working on an existing project that is already developed. There is a method that is providing some functionality and it is invoked from many places. As per the new requirement, we have to make some new changes in the existing method. To provide a new functionality we have to send one more parameter in the existing method.  But need to take care of existing functionality because the method is already invoked from many locations. In these types of situations, we should overload the method so that we will not break the existing code.

When to use dynamic polymorphism

Method overriding is only possible in inheritance. When you want to provide specific implementation to an inherited method. Then you should use the method overriding.
You can provide the implementation of the method of derived class without even modifying the parent class code.
According to the concept of polymorphism while writing the code we should use of generic interface instead of concrete implementation. Because it provides an ease to add new implementation if the requirement arises in the future.

In Java, AbstractCollection class provides the generic interface for all the classes that extend it. AbstractCollection class has a various method that is overridden by classes according to their use.
We will discuss the add(E e) method that defined in the AbstractCollection class. But ArrayList and HashSet provide their body according to their use.

polymorphism real time example

Leave a Comment