Careers360 Logo
Types of Polymorphism in Java [Static & Dynamic Polymorphism with Examples]

Types of Polymorphism in Java [Static & Dynamic Polymorphism with Examples]

Edited By Team Careers360 | Updated on Apr 06, 2022 03:42 PM IST

What is Polymorphism in Java?

Polymorphism is a key notion in Object-Oriented Programming (OOP), which means that a single action can be executed in a variety of ways. It comes from the Greek terms poly and morphs, which indicate "many" and "forms." When they are related by heredity, different forms occur.

Polymorphism is similar to inheritance in that it applies methods and attributes from a separate class and performs various tasks. Polymorphism is the ability of an object to mould itself into different shapes.

That is, an entity can carry out various operations in various conditions. When a parent class reference is used to refer to a child class object, this is one of the most prevalent uses of polymorphism in Object-Oriented Programming. More information on OOPs concepts and examples may be found here.

In Java, there are three ways to implement polymorphism:

Method Overriding: When a superclass and a subclass both have the same method, method overriding refers to the process of the subclass's method overriding the superclass's method. As a result, the same procedure will be employed in many situations and for various operations.

The output of the program:

Java Programming Language

Common English Language

Method Overloading: Method overloading refers to process of creation of methods with the same name but with different parameters. Examples include: void func() { … }, void func(int a) { … }, float func(double a) { … }, float func(int a, float b) { … }.

Output

**********

##########

Operator overloading: Operator overloading is a concept in Java where an operator behaves differently with different operands. Examples include:

The operator “+” can be used both in numeric addition and string concatenation.

The operators &,|, and ! can be used for logical and bitwise overloading.

Types of Polymorphism in Java

The four basic concepts of Object-Oriented Programming are abstraction, encapsulation, inheritance, and polymorphism. The ability to process objects differently depending on their class and data types is known as polymorphism.

Polymorphism in Java is divided into two types: compile time polymorphism and run time polymorphism. Static polymorphisms and dynamic polymorphisms are terms used to describe this type of java polymorphism.

1. Static polymorphism (or compile-time polymorphism)

Polymorphism in Java, like most other OOP programming languages, allows for the inclusion of several methods within a single class. Although the methods have the same name, the parameters differ. The static polymorphism is represented by this. This polymorphism is achieved through method overloading and is resolved during the compiler time. There are three conditions by which the parameter sets must differ:

The number of parameters should be varied.

Different parameter types should be used.

The parameters are in a different sequence. If one method accepts a string and a long while the other accepts a long and a string, for example. This form of order, on the other hand, makes it tough for the API to grasp.

Every method has a different signature due to the differences in parameters. The Java compiler knows which method is invoked.

Example of static polymorphism

One of the ways by which Java supports static polymorphism is method overloading. An example showing the case of method overloading in static polymorphism is shown below:

Example:

class SimpleCalculator

{

int add(int a, int b)

{

return a+b;

}

int add(int a, int b, int c)

{

return a+b+c;

}

}

public class Demo

{

public static void main(String args[])

{

SimpleCalculator obj = new SimpleCalculator();

System.out.println(obj.add(25, 25));

System.out.println(obj.add(25, 25, 30));

}

}

Output of the program

50

80

2. Dynamic Polymorphism (or run time polymorphism in Java)

The compiler does not determine the method to be executed in this type of polymorphism in Java. The process is carried out at runtime by the Java Virtual Machine (JVM). When a call to an overridden process is resolved at run time, it is referred to as dynamic polymorphism. The overridden method is called by a superclass's reference variable. While the methods implemented by both the subclass and the superclass have the same name, they provide separate functionality.

Before you can understand the concept of run time polymorphism, you must first understand the process of upcasting. Upcasting is the process of referring to a child class object with a reference variable from the superclass.

Example of Dynamic polymorphism (or run time)

Example1:

The classes Bike and Splendor are created, with the Splendor class extending Bike and overriding its run() method. The parent class's reference variable invokes the run() method. Because the subclass method is overriding the parent class method, it is called at run time.

The program

class Bike{

void run(){System.out.println(“running”);}

}

class Splendor extends Bike{

void run(){System.out.println(“walking safely with 30km”);}

public static void main(String args[]){

Bike b = new Splendor();//upcasting

b.run();

}

}

Output: walking safely with 60km

Example 2

Two classes with the names of “ABC” and “XYZ” are created where XYZ is a child class and ABC is a parent class. Here, the method myMethod() of the parent class is overridden by the child class. The child class object is assigned to the parent class reference.

Program:

class ABC{

public void myMethod(){

System.out.println(“Overridden Method”);

}

}

public class XYZ extends ABC{

public void myMethod(){

System.out.println(“Overriding Method”);

}

public static void main(String args[]){

ABC obj = new XYZ();

obj.myMethod();

}

}

Output

Overriding Method

Conclusion

Polymorphism is one of the most useful characteristics of Object-Oriented Programming in Java. Furthermore, the ability to call a method both statically and dynamically broadens its applicability. The concept of polymorphism was introduced with the intention of providing flexibility. Additionally, the ability to process a large number of objects in a single reference variable simplifies coding. As a result, the concept should be applied to daily coding jobs and its effectiveness embraced.

Articles

Get answers from students and experts
Back to top