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.
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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
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.
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.
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.
It means allowing access to any object in any of these below ways given as under –
Binding means the connection of a method call to a method body. There exist two kinds of binding:
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.
Given below are the different examples of Polymorphism in Java:
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:
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:
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:
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.
The above is the detailed content of Polymorphism in Java. For more information, please follow other related articles on the PHP Chinese website!