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.
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.
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.
The above is the detailed content of Polymorphism in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
