Open In App

Compile Time Polymorphism in Java

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Polymorphism in Java refers to an object’s capacity to take several forms. Polymorphism allows us to perform the same action in multiple ways in Java.

Polymorphism is divided into two types:

  • Compile-time polymorphism
  • Run time polymorphism

Note: Run time polymorphism is implemented through Method overriding. Whereas Compile Time polymorphism is implemented through Method overloading and Operator overloading.

In this article, we will see Compile-time polymorphism.

Compile-time Polymorphism

Compile-time polymorphism is also known as static polymorphism or early binding. Compile-time polymorphism is a polymorphism that is resolved during the compilation process. Overloading of methods is called through the reference variable of a class. Compile-time polymorphism is achieved by method overloading and operator overloading.

1. Method overloading

We can have one or more methods with the same name that are solely distinguishable by argument numbers, type, or order.

Method Overloading occurs when a class has many methods with the same name but different parameters. Two or more methods may have the same name if they have other numbers of parameters, different data types, or different numbers of parameters and different data types. 

Example: 

void gfg() { ... }
void gfg(int num1 ) { ... }
void gfg(float num1) { ... }
void gfg(int num1 , float num2 ) { ... }

a. Method overloading by changing the number of parameters 

 In this type, Method overloading is done by overloading methods in the function call with a varied number of parameters.

 Example:

show( char a )
show( char a ,char b )

 In the given example, the first show method has one parameter, and the second show method has two methods. When a function is called, the compiler looks at the number of parameters and decides how to resolve the method call.

Java
// Java program to demonstrate the working of method
// overloading by changing the number of parameters 

public class MethodOverloading {
    
      // 1 parameter
    void show(int num1)
    {
        System.out.println("number 1 : " + num1);
    }

    // 2 parameter
    void show(int num1, int num2)
    {
        System.out.println("number 1 : " + num1
                           + "  number 2 : " + num2);
    }

    public static void main(String[] args)
    {
        MethodOverloading obj = new MethodOverloading();
      
          // 1st show function
        obj.show(3); 
      
          // 2nd show function
        obj.show(4, 5); 
    }
}

Output
number 1 : 3
number 1 : 4  number 2 : 5

In the above example, we implement method overloading by changing several parameters. We have created two methods, show(int num1) and show(int num1, int num2). In the show(int num1) method display, one number and the void show(int num1, int num2) display two numbers.

b. Method overloading by changing Datatype of parameter

In this type, Method overloading is done by overloading methods in the function call with different types of parameters.

Example:

show( float a float b)
show( int a, int b )

In the above example, the first show method has two float parameters, and the second show method has two int parameters. When a function is called, the compiler looks at the data type of input parameters and decides how to resolve the method call.

Program:

Java
// Java program to demonstrate the working of method
// overloading by changing the Datatype of parameter

public class MethodOverloading {
  
    // arguments of this function are of integer type
    static void show(int a, int b)
    {
        System.out.println("This is integer function ");
    }
  
    // argument of this function are of float type
    static void show(double a, double b)
    {
        System.out.println("This is double function ");
    }
  
    public static void main(String[] args)
    {
        // 1st show function
        show(1, 2);
      
        // 2nd show function
        show(1.2, 2.4);
    }
}

Output
This is integer function 
This is double function 

In the above example, we changed the data type of the parameters of both functions. In the first show() function datatype of the parameter is int. After giving integer type input, the output will be ‘ This is integer function.’ In the second show() function datatype of a parameter is double. After giving double type input, the output would be ‘This is double function.’ 

c. By changing the sequence of parameters 

In this type, overloading is dependent on the sequence of the parameters. 

Example:

show( int a, float b ) 
show( float a, int b )

Here in this example, The parameters int and float are used in the first declaration. The parameters are int and float in the second declaration, but their order in the parameter list is different.

Java
// Java program to demonstrate the working of method
// overloading by changing the sequence of parameters

public class MethodOverloading {

    // arguments of this function are of int and char type
    static void show(int a, char ch)
    {
        System.out.println("integer : " + a
                           + " and character : " + ch);
    }

    // argument of this function are of char and int type
    static void show(char ch, int a)
    {
        System.out.println("character : " + ch
                           + " and integer : " + a);
    }

    public static void main(String[] args)
    {
        // 1st show function
        show(6, 'G');

        // 2nd show function
        show('G', 7);
    }
}

Output
integer : 6 and character : G
character : G and integer : 7

In the above example, in the first show, function parameters are int and char, and in the second shoe, function parameters are char, and int. changed the sequence of data type. 

Invalid cases of method overloading:

Method overloading does not allow changing the return type of method( function ); it occurs ambiguity.

Examples

int sum(int, int);
String sum(int, int);

Because the arguments are matching, the code above will not compile. Both methods have the same amount of data types and the same sequence of data types in the parameters.

2. Operator Overloading 

Note: Operator Overloading is not supported in Java, in the following example we’re trying to achieve the same functionality by the use of methods.

An operator is said to be overloaded if it can be used to perform more than one function other than the one its pre-defined for. Operator overloading is a mechanism through which we can change the meaning of a pre-defined operator and make it work for user-defined objects. In this example, we’ll try to achieve the same by the use of method as Java does NOT support operator overloading.

Java
// Java program to demonstrate the
// working of operator overloading

public class GFG {

    // function for adding two integers
    void add(int a, int b)
    {
        int sum = a + b;
        System.out.println(" Addition of two integer :"
                           + sum);
    }

    // function for concatenating two strings
    void add(String s1, String s2)
    {
        String con_str = s1 + s2;
        System.out.println("Concatenated strings :"
                           + con_str);
    }

    public static void main(String args[])
    {
        GFG obj = new GFG();
      
        // addition of two numbers
        obj.add(10, 10);
      
        // concatenation of two string
        obj.add("Operator ", " overloading ");
    }
}

Output
 Addition of two integer :20
Concatenated strings :Operator  overloading 

In the above example, the add method performs the addition between two strings achieving the same functionality as if ‘+’ operator has been overloaded.

Advantages of Compile-time Polymorphism:

  • It improves code clarity and allows for the use of a single name for similar procedures.
  • It has a faster execution time since it is discovered early in the compilation process.

Disadvantages of Compile-time Polymorphism:

  • Limited dynamic behavior: It limits the dynamic behavior & flexibility of our code, allowing modifications only before the code is executed, hence it must be used by keeping certain factors in mind.
  • Maintenance issues: As the codebase grows, it can be quite difficult to manage and make any new modifications if we have too many overloaded methods.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads