public class Calculator {
public int sum(int a, int b) {
return a + b;
}
public int sum(int a, int b, int c) {
return a + b + c;
}
}
What is polymorphism in Java OOP
Upasana | May 24, 2019 | 2 min read | 1 views
Polymorphism
It means one name many forms. It is further of two types - static and dynamic. Static polymorphism is achieved using method overloading and dynamic polymorphism using method overriding.
Static polymorphism
Static polymorphism is achieved using method overloading. Method overloading is a programming technique that allows developers to use the same method name multiple times in the same class, but with different parameters.
There are mainly two variations of method overloading:
-
implementing two or more methods that have the same name but take different numbers of arguments
-
implementing two or more methods that have the same name but take arguments of different types
Example
As we can see that method overloading makes code clean and easier to read.
Dynamic polymorphism
Dynamic polymorphism is achieved in Java using method overriding. It is closely related to inheritance. We can write a code that works on the superclass, and it will work with any subclass type as well.
Example
Java collections framework has an interface called java.util.Collection
, ArrayList
and TreeSet
are two different implementation of this interface. ArrayList maintains the insertion order of elements while TreeSet
orders its elements by their natural order
or comparator
(if supplied). Now if we write a method that accepts a collection and prints its elements, the actual object (ArrayList or TreeSet) at runtime will decide the behavior of this method.
public void print(Collection<String> collection) {
for (String s : collection) {
System.out.println("s = " + s);
}
}
Collection<String> collection1 = new ArrayList<>();
collection1.add("A");
collection1.add("D");
collection1.add("B");
collection1.add("C");
print(collection1); (1)
1 | elements will be printed as per the insertion order of elements into arraylist |
s = A s = D s = B s = C
Collection<String> collection2 = new TreeSet<>();
collection2.add("A");
collection2.add("D");
collection2.add("B");
collection2.add("C");
print(collection2); (1)
1 | elements will be printed as per the natural order |
s = A s = B s = C s = D
We just saw that print()
method’s behavior is determined by the actual type of object passed to it at run time. That’s polymorphism!
-
Other than objects of type
java.lang.Object
, all java objects are polymorphic i.e. they pass the IS-A test for their own type as well as for classObject
. -
A reference variable’s type determines the methods that can be invoked on the object that variable is referencing to. In the example above,
print()
method can only invoke methods that are listed onCollection
interface irrespective the type of actual object passed to this method. -
Polymorphic method invocation applies only to the instance methods (not to static methods, not to variables). Only overriden instance methods are dynamically invoked based on the real object’s type at runtime.
Top articles in this category:
- What are four principles of OOP, How aggregation is different than Composition?
- Is Java Pure Object Oriented Language?
- What do you understand by Java Memory Model?
- What is difference between sleep() and wait() method in Java?
- What is purpose of Collections.unmodifiableCollection
- What is volatile keyword in Java
- What is Immutable Class in Java