Polymorphism and Dynamic Binding in Java

Polymorphism is an Object-Oriented-Programming concept. Whether you are new to Java programming or a person who has worked with Java for years, you should know what polymorphism is in Java and how it works. Most developers claim that they know the topic well but when it comes to other complex features like static and dynamic binding, they seem underconfident. A good understanding of polymorphism allows us to build efficient object-oriented programs.

What Is Polymorphism in Java?

Polymorphism is composed of two words:

  • Poly: meaning “many”
  • Morphism: meaning “forms”

Thus polymorphism means having many forms. In the programming world, it is defined as the capability of a signal or message to be displayed in more than one form.

Real-Life Example

A person can exhibit many characteristics at the same time. For example, a mother at the same time can be a wife, daughter, sister, employee of a company, etc. Hence, a person can exhibit different characteristics under different conditions. This is known as polymorphism.

Diagram displaying all the identities a person can have

Importance of Polymorphism

Polymorphism is one of the most important features of any object-oriented programming language (for example, Java), With the help of polymorphism, a single task can be carried out in different ways.

Types of Polymorphism

In Java, polymorphism is broadly classified into two categories:

  1. Compile-time polymorphism (static binding)
  2. Runtime polymorphism (dynamic binding)

Compile-Time Polymorphism

Compile-time polymorphism is also known as static binding. This type of polymorphism can be achieved through function overloading or operator overloading. But in Java, it is restricted to function overloading as Java doesn’t provide the support to carry out operator overloading.

Function Overloading

When there are at least two functions or methods with the same function name but either the number of parameters they contain is different or at least one data type of the corresponding parameter is different (or both), then it is known as a function or method overloading and these functions are known as overloaded functions.

Example 1

Untill now, we have studied what function overloading is. Now let’s try to demonstrate a function overloading programmatically

class Main {
 
    // Method 1
    // Method with 2 integer parameters
    static int Addition(int a, int b)
    {
 
        // Returns sum of integer numbers
        return a + b;
    }
    // Method 2
    // having the same name but with 2 double parameters
    static double Addition(double a, double b)
    {
        // Returns sum of double numbers
        return a + b;
    }
    public static void main(String args[]) {
        
        // Calling method by passing
        // input as in arguments
        System.out.println(Addition(12, 14));
        System.out.println(Addition(15.2, 16.1));
 
    }
}

You can run the above program from here.

Program explanation:

  • The above program consists of two static functions having the same name: Addition.
  • Here, both function contains the same number of parameters but their corresponding parameters are different.
  • Method 1 accepts two integration parameters whereas Method 2 accepts two parameters of double data type.
  • From the main function, we are calling the Addition(12, 14) function first. The parameters passed are integers (12 and 14), hence Method 1 would be called here.
  • Then we have called the Addition(15.2, 16.1) function. Since the parameters passed are double data type (15.2 and 16.1), Method 2 would be called this time.
  • This is how function overloading is achieved in Java on the basis of different data types of parameters.

Example 2

Consider the program given below:

class Main {
 
    // Method 1
    // Method with 2 integer parameters
    static int Addition(int a, int b)
    {
 
        // Returns sum of integer numbers
        return a + b;
    }
 
    // Method 2
    // having the same name but with 3 integer parameters
    static double Addition(double a, double b)
    {
 
        // Returns sum of integer numbers
        return a + b;
    }
    public static void main(String args[]) {
        
        // Calling method by passing
        // input as in arguments
        System.out.println(Addition(12, 14));
        System.out.println(Addition(15.2, 16.1));
 
    }
}

You can run the above program from here.

Program explanation:

  • The above program consists of two static functions with the same name: Addition.
  • Here, both functions contain a different number of parameters but the data type of the first two corresponding parameters is the same (integer).
  • Method 1 accepts two integration parameters and Method 2 accepts three parameters of integer data type.
  • From the main function, we are calling the Addition(2, 3) function first. Since the parameters passed are integers (2 and 3), Method 1 would be called here.
  • Then we have called the Addition(4, 5, 6) function. The parameters passed are double data types (4, 5, 6), so Method 2 would be called this time.
  • This is how function overloading is achieved in Java on the basis of a different number of parameters.

Example 3

class Main {
 
    // Method 1
    // Method with 2 integer parameters
    static int Addition(int a, int b)
    {
        // Return the sum
        return a + b;
    }
    // Method 2
    // having the same name but with 3 parameters
    // 1st parameter is of type double and other parameters
    // are of type integer
    static double Addition(double a, int b,  int c)
    {
        // Return the sum
        return a + b + c;
    }
    public static void main(String args[]) {
        
        // Calling method by passing
        // input as in arguments
        System.out.println(Addition(2, 4));
        System.out.println(Addition(4.2, 6, 10));
 
    }
}

You can run the above program from here.

Program explanation:

  • The above program consists of two static functions having the same name: Addition.
  • Both functions contain a different number of parameters and the data type of the first corresponding element is also different.
  • Method 1 accepts two integration parameters whereas Method 2 accepts three parameters — the first one is a double type and the other two are integer data types.
  • From the main function, we are calling the Addition(2, 4) function first. Since the parameters passed are integers (2 and 4), Method 1 would be called here.
  • Then we have called the Addition(4.2, 6, 10) function. The first parameter passed is an integer type and other parameters are double data type (4.2, 6, 10), therefore, Method 2 would be called this time.
  • This is how function overloading is achieved in Java on the basis of a different number of parameters as well as the different data types of corresponding parameters.

Note: Function cannot be overloaded on the basis of the return type of functions only.

Runtime Polymorphism

This is also known as dynamic binding. In this process, a function call made to a function is resolved during runtime only. We can achieve dynamic binding in Java through method overriding.

Method Overriding

Method overriding in Java occurs when a method in the base class has a definition in the derived class. The method or function in the base class is known as an overridden method.

// Class 1
class Parent {
 
    // Print method
    void Print()
    {
 
        // Print statement
        System.out.println("Inside Parent Class");
    }
}
 
// Class 2
class Child1 extends Parent {
 
    // Print method
    void Print() { System.out.println("Inside Child1 Class"); }
}
 
// Class 3
class Child2 extends Parent {
 
    // Print method
    void Print()
    {
        // Print statement
        System.out.println("Inside Child2 Class");
    }
}
 
class Main {
 
    public static void main(String args[]) {
        
        // Creating an object of class Parent
        Parent parent = new Parent();
        parent.Print();
 
        // Calling print methods
        parent = new Child1();
        parent.Print();
 
        parent = new Child2();
        parent.Print();
    }
}

You can run the above program from here.

Program explanation:

  • The above program consists of three classes: Parent (class 1), Child1 (class 2), and Child2 (class 3). class 2 and class 3 inherit class 1.
  • The Parent has a method called Print(). Inside this function, we are printing "Inside Parent Class". Child1 and Child2 also have Print() functions that basically override the Print() function of the class Parent and would print "Inside Child1 Class" and "Inside Child2 Class" respectively to the console.
  • From the main function, we are first creating an object of the parent class called a parent. Then, with the help of this object, we are calling the print method of the parent class. Therefore, "Inside Parent Class" would be printed on the console.
  • After that, we have called the default constructor of the Child1 class and we are calling the Print() function. Note that now the Print() method defined under the Child1 class would be called as we have overridden the Print() method of the parent class. Hence, "Inside Child1 Class" would be printed on the console.
  • Lastly, we have called the default constructor of the Child2 class and we are calling the Print() function. Here, the Print() method defined under the Child2 class would be called as we have overridden the Print() method of the parent class. Hence, “Inside Child2 Class” would be printed on the console.
  • This is how method overriding is achieved in Java.

Summary

In this article, we learned what polymorphism is in Java. Then we went deep into the topic and discussed two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We demonstrated through programs how we can achieve static and dynamic binding in Java.

.

Leave a Comment