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

What is Polymorphism in Java?

One of the important concepts in Object Oriented Programming (OOP) is polymorphism which means that a single action can be performed in different ways. It is derived from the Greek words: poly and morphs meaning many and forms. The different forms exist when they are related through inheritance.

Like inheritance that inherits methods and attributes from a different class, polymorphism applies those methods and performs different tasks. An object is able to mold itself into various forms through polymorphism.

That is, an entity can perform different operations in different scenarios. One of the most common uses of polymorphism in Object Oriented Programming is when a parent class reference is used to refer to a child class object. Read more on various OOPs concepts and examples.

Polymorphism in Java can be achieved through three methods:

Method Overriding: If a superclass and a subclass consist of the same method, then method overriding refers to the process of overriding the method in the superclass by the method in the subclass. As a result, the same method will be used in different conditions performing different operations.

Output of the program:

Java Programming Language

Common English Language

  1. 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

**********

##########

  1. 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.
Numerical addition String concatenation
int a = 5;

int b = 6;

int sum = a + b;  // Output = 11

String first = “Python “;

String second = “Programming”;

name = first + second;  // Output = Python Programming

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

Types of Polymorphism in Java

The Object Oriented Programming focuses on four basic concepts i.e. abstraction, encapsulation, inheritance, and polymorphism. Polymorphism is the ability to process objects differently on the basis of their class and data types.

There are two types of polymorphism in Java: compile time polymorphism and run time polymorphism in java. This java polymorphism is also referred to as static polymorphisms and dynamic polymorphisms.

1. Static polymorphism (or compile-time polymorphism)

Like most of the other OOP programming languages, Java polymorphism allows the incorporation of multiple methods within a class. The methods use the same name but the parameter varies. This represents the static polymorphism. This polymorphism is resolved during the compiler time and is achieved through the method overloading. Three criteria are provided in which the parameter sets have to differ:

  • The parameters number should vary.
  • The parameter types should be different.
  • Different order of parameters. For example, if a method accepts a string and a long, while the other method accepts a long and a string. However, this type of order makes it difficult for the API to understand.

Due to the difference in the parameters, every method has a different signature. The Java compiler has an idea of which method is called.

Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

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)

In this form of polymorphism in java, the compiler doesn’t determine the method to be executed. It’s the Java Virtual Machine (JVM) that performs the process at the run time. Dynamic polymorphism refers to the process when a call to an overridden process is resolved at the run time. The reference variable of a superclass calls the overridden method. The methods that are implemented by both the subclass and the superclass provide different functionality while sharing the same name.

Understanding the process of Upcasting is crucial before understanding the concept of run time polymorphism. Upcasting refers to the process where an object of the child class is referred to by a reference variable of the superclass. Learn about polymorphism in PHP.

Example of Dynamic polymorphism (or run time)

Example1:

Two classes Bike and Splendor are created and the class of Splendor extends the class of Bike overriding its run() method. The run() method is called by the reference variable of the parent class. Since the subclass method is overriding the parent class method; the subclass method is invoked 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

The concept of polymorphism in java is one of the most useful features in Object oriented programming.  Further, the capability to invoke a method both at the static and the run time widens its applicability.

The idea behind bringing the concept of polymorphism is flexibility. Further the ability to process a large number of objects in a single reference variable, makes the coding a bit easier. Therefore, the concept should be used in the daily coding tasks and embrace its effectiveness.

If you want to learn more about software technology, its development, and the mechanism behind it, you can check the course Executive PG Programme in Software Development – Specialisation in Full Stack Development offered by upGrad. The specialization course is a 23 weeks online program offering over 300+ case studies to boost up your knowledge and available tools and programming language to enhance your practical skills. If you have any more queries related to the course, drop us a message. Our team will contact you.

Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
Learn More

0 replies on “Types of Polymorphism in Java [Static & Dynamic Polymorphism with Examples]”

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses

×