Java - Types of Polymorphism and Advantages - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to understand the concept of polymorphism in Java and different types of it.
Polymorphism is a significant feature of Object Oriented Principles. The word polymorphism came from two Greek words ‘poly‘ and ‘morphs‘. Here poly means many and morphs means forms.
Polymorphism represents the ability of an object to assume different forms. Therefore, it enables programmers to create/write programs that are easier to understand and reuse.
Certainly, Polymorphism provides flexibility to the programmer to write programs that use a single method for different operations depending on the requirement.
 In Java, we can define a single interface to represent multiple concrete classes. So by accepting the interface type, a method can behave differently for different inputs.
Java supports two types of polymorphism namely:

Compile-time polymorphism
Run-time polymorphism.

Compile-time polymorphism
Run-time polymorphism.
Compile-time polymorphism is the type of polymorphism occurs when the compiler compiles a program. Java supports compile-time polymorphism through method overloading. The other names of Compile-time polymorphism are static polymorphism or early binding.
Polymorphism occurs in method overloading because method overloading allows access to different methods through the same interface.
In method overloading, two or more methods in a class can use the same name as long as their parameter declarations are different.
When the compiler encounters a call to an overloaded method, it identifies the correct version of the overloaded method by comparing the type and the number of arguments.
Example:
public class OverloadingDemo {
    public void meth(int value) {
        System.out.println("int value : " + value);
    }
    public void meth(String name) {
        System.out.println("String value : " + name);
    }
    public void meth(int value, String name) {
        System.out.println("Name with value : " + value+" "+name);
    }
    public static void main(String[] args) {
        OverloadingDemo demo = new OverloadingDemo();
        demo.meth(10);
        demo.meth("online tutorials point");
        demo.meth(20, "Overloading Demo");
    }
}
Output:
int value : 10
String value : online tutorials point
Name with value : 20 Overloading Demo
Run-time Polymorphism is the type of polymorphism that occurs dynamically when a program executes. Java supports run-time polymorphism by dynamically dispatching methods at run time through method overriding.
For this type of polymorphism, method invocations are resolved at run time by the JVM and not at the compile time.
Since the run-time polymorphism occurs dynamically, it is also called dynamic polymorphism or late binding.
interface Car {
    void speed(int speed);
}
class Maruti implements Car {
    public void speed(int speed) {
        System.out.println("Maruti Speed is :" + speed);
    }
}
class Tata implements Car {
    public void speed(int speed) {
        System.out.println("Tata Speed is :" + speed);
    }
}
public class PolymorphismDemo {
    public static void main(String[] args) {
        Car car;
        car = new Maruti();
        car.speed(200);
        car = new Tata();
        car.speed(300);
    }
}
Output:
Maruti Speed is :200 
Tata Speed is :300
In the above example – Maruthi and Tata are two different classes, and both were two different implementations of the Car Interface. In other words, these two classes are types of Car.
Since because these two classes are the same type, we can refer these two objects to a single type called Car as above. Here, based on the type of the object being assigned with the reference, polymorphism is resolved at runtime.

Method overloading allows methods that perform similar or closely related functions to be accessed through a common name. For instance, a program performs operations on an array of numbers which can be int, float, or double type. Method overloading allows you to define three methods with the same name and different types of parameters to handle the array of operations.

Method overloading allows methods that perform similar or closely related functions to be accessed through a common name. For instance, a program performs operations on an array of numbers which can be int, float, or double type. Method overloading allows you to define three methods with the same name and different types of parameters to handle the array of operations.

Constructors allowing different ways to initialize objects of a class can implement method overloading. This enables you to define multiple constructors for handling different types of initializations.

Constructors allowing different ways to initialize objects of a class can implement method overloading. This enables you to define multiple constructors for handling different types of initializations.

Method overriding allows a subclass to use all the general definitions that a superclass provides and add specialized definitions through overridden methods.

Method overriding allows a subclass to use all the general definitions that a superclass provides and add specialized definitions through overridden methods.

Method overriding works together with inheritance to enable code-reuse of existing classes without the need for re-compilation.

Method overriding works together with inheritance to enable code-reuse of existing classes without the need for re-compilation.

Method overloading occurs at Compile-time whereas method overriding occurs at Run-time.

Method overloading occurs at Compile-time whereas method overriding occurs at Run-time.

Method overloading occurs when the type signatures of the two methods are different whereas method overriding occurs only when the type signatures of the two methods are the same.

Method overloading occurs when the type signatures of the two methods are different whereas method overriding occurs only when the type signatures of the two methods are the same.

In method overloading, the compiler calls the correct method by comparing the type signatures. However, in method overriding, the JVM determines the correct method based on the object that the invoking variable is referring to.

In method overloading, the compiler calls the correct method by comparing the type signatures. However, in method overriding, the JVM determines the correct method based on the object that the invoking variable is referring to.

Method overloading is possible on methods with private, static, and final access modifiers. On the other hand, method overriding is not possible on these access modifiers.

Method overloading is possible on methods with private, static, and final access modifiers. On the other hand, method overriding is not possible on these access modifiers.
Object Oriented Principles
Method Overriding and Overloading
Happy Learning 🙂

Overriding vs Overloading in Java
Oops Concepts In Java
Types Of Inheritance in Java
Advantages of Exception Handling
What is Java Constructor and Types of Constructors
Java Main Method – public static void main(String args[])
Advantages of Java Programming
Java variable types Example
Spring Bean Autowire ByType Example
Factory Method Pattern in Java
Top 40 Core Java Interview Questions and Answers
OOPS – Java Access Modifiers
Java Class Example Tutorials
Hello World Java Program Example
What are different Python Data Types

Overriding vs Overloading in Java
Oops Concepts In Java
Types Of Inheritance in Java
Advantages of Exception Handling
What is Java Constructor and Types of Constructors
Java Main Method – public static void main(String args[])
Advantages of Java Programming
Java variable types Example
Spring Bean Autowire ByType Example
Factory Method Pattern in Java
Top 40 Core Java Interview Questions and Answers
OOPS – Java Access Modifiers
Java Class Example Tutorials
Hello World Java Program Example
What are different Python Data Types






Sravanthi
November 3, 2017 at 2:38 pm - Reply 

Good info but in Advantages can be made like hitting bulls eye.
simpe and short like code reuse,Better Performance, optimized way of achieving tasks etc.









priya
May 11, 2018 at 5:58 am - Reply 

The basic reason behind polymorphism is that we can implement different behaviors of the same object depending upon the reference type passed to an object.










Sravanthi
November 3, 2017 at 2:38 pm - Reply 

Good info but in Advantages can be made like hitting bulls eye.
simpe and short like code reuse,Better Performance, optimized way of achieving tasks etc.




Good info but in Advantages can be made like hitting bulls eye.
simpe and short like code reuse,Better Performance, optimized way of achieving tasks etc.





priya
May 11, 2018 at 5:58 am - Reply 

The basic reason behind polymorphism is that we can implement different behaviors of the same object depending upon the reference type passed to an object.




The basic reason behind polymorphism is that we can implement different behaviors of the same object depending upon the reference type passed to an object.