Introduction to Polymorphism in Java
Polymorphism is defined as a concept wherein, along with one action in numerous ways, it could be performed. The word was originated from the 2 Greek words, which are poly as well as morphs. Here, as you might know, “poly” implies many, as well as “morphs” implies many forms. Therefore, the word polymorphism would imply many forms. Let us now understand Polymorphism in java in detail.
How does Polymorphism work in Java?
Polymorphism is an OOO programming characteristic. Whenever we use it, a class has got the ability to exhibit many functionalities even if they exhibit the common interface. Therefore, we can assume the long word actually tells a very simple concept.
The thing worth noting about polymorphism is that the entire working code in numerous classes does not really need to understand the class that is getting used by it in the same way of usage.
Let us assume the real-world example of polymorphism. Let us assume the button. You all know that if we apply a little bit of pressure, you will be able to press the button; however, we won’t be knowing the output of putting pressure on the button and the purpose of the use. So, the point here to take care of is that either way, the result won’t be affecting the procedure that is being used.
So the basic goal of Polymorphism is to make objects which are interchangeable depending on the needs.
Types of Polymorphism in Java
- The two types of polymorphism are compile-time polymorphism and run-time polymorphism. Compile-time polymorphism is performed by method overloading and runtime polymorphism by method overriding.
- Runtime polymorphism, also called Dynamic Method Dispatch, is a process wherein a call to an overridden method is resolved at runtime despite at compile-time. Herein, the overridden method is called through a reference variable of a parent class. Also, Runtime polymorphism cannot be achieved by data members.
Importance
If we discard dynamic polymorphism as well as static polymorphism, in programming exists some of the programming characteristics of Java which exhibits polymorphism that is other than these two important types.
They are these – Coercion, Operator Overloading, and Polymorphic Parameters.
1. Coercion
Let’s try to get the meaning of coercion through this example. Assume there exists the value of the string to be co value: Assume the second number that has got the value 2. Now, what would occur when you will concat this string value along with this number? The outcome that we get out of this concatenation would be “co value: 2”. This is known as Coercion. This is a conversion that is of type implicit, which is executed to prevent errors.
2. Operator Overloading
As we start with the concept of operator overloading, let me glance at one scenario. Assume the string has got the value “Operate” and 2nd that has got value as “Overload”. Then we would utilize plus symbol (+) along the same way the addition of 2 numbers. This (+) would be concatenating. If we consider two integers, then adding these two numbers would be returned. Whenever one symbol or operator has the ability to alter the interpretation considering the procedure used, the polymorphism type that gets done is known as Operator Overloading.
3. Polymorphic Parameters
It means allowing access to any object in any of these below ways given as under –
- Usage of a reference variable that belongs to either parent class.
- Usage of a reference variable that belongs to the class where that exists.
4. Polymorphism Scope
Binding means the connection of a method call to a method body. There exist two kinds of binding:
- 1st is Static binding which means whenever the type of object has been determined during compile-time.
- 2nd is Dynamic binding which means whenever the type of object has been determined during run-time.
The sole reason as to why Polymorphism is required lies as its concept is enormously needed in the implementation of inheritance. It also plays a vital role in allowing the objects to inherit numerous structures in sharing the interface. Polymorphism has been mentioned clearly as the only one that is mapped for many.
Examples of Polymorphism in Java
Given below are the different examples of Polymorphism in Java:
Example #1
Java program to showcase Method Overloading.
Code:
class methodOverload {
static int multi(int x, int y)
{
return x * y;
}
// Method with the same name but with 2 double arguments
static double multi(double x, double y)
{
return x * y;
}
}
class Main {
public static void main(String[] args)
{
System.out.println(methodOverload.multi(6, 2));
System.out.println(methodOverload.multi(1.2, 1.2));
}
}
Output:
Example #2
Java program to showcase Operator Overloading.
Code:
class operator {
void oper(String s, String s2)
{
String s = s + s2;
System.out.println("Concatenated String is"
+ s);
}
void oper(int a, int b)
{
int c = a + b;
System.out.println("Sum is : " + c);
}
}
class Main {
public static void main(String[] args)
{
operator o = new operator();
o.oper(1, 2);
o.oper("hi", "bye");
}
}
Output:
Example #3
Java program for Method Overloading.
Code:
class Multi {
// Method with 2 parameter
static int Multi(int a, int b)
{
return a * b;
}
static int Multi(int a, int b, int c)
{
return a * b * c;
}
}
class Main {
public static void main(String[] args)
{
System.out.println(Multi.Multi(2, 3));
System.out.println(Multi.Multi(2, 3, 3));
}
}
Output:
Conclusion
The sole reason as to why Polymorphism is required lies as its concept is enormously needed in the implementation of inheritance. It also plays a vital role in allowing the objects to inherit numerous structures in sharing the interface. Polymorphism has been mentioned clearly as the only one that is mapped for many.
Recommended Articles
This is a guide to Polymorphism in Java. Here we discuss the working of polymorphism in Java with its types, importance, and different examples and code implementation. You may also look at the following articles to learn more –