Compile Time Polymorphism in Java (original) (raw)

Compile-time polymorphism also known as static polymorphism or early binding, is a type of polymorphism where the method call is resolved by the compiler during compilation. It allows multiple methods to have the same name but different parameter lists.

How to Achieve Compile-Time Polymorphism

Compile-time polymorphism can be achieved through:

**Method overloading

Method overloading is a feature that allows multiple methods in the same class to have the same name but different parameter lists. The compiler determines which method to call based on the number, type, or order of arguments.

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

**Syntax:

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

Java `

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

**Explanation: 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.

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

**Syntax:

show(int a, int b)
show(double a, double b)

Java `

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

**Explanation :The compiler calls the integer version when integer values are passed and the double version when double values are passed.

**By changing the **sequence of parameters

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

**Syntax:

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

Java `

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

**Explanation: Although both methods have the same parameter types, their sequence differs. The compiler selects the method according to the order of arguments.

**Invalid cases of method overloading

Changing only the return type of a method does not create a valid overloaded method because it causes ambiguity.

**Invalid Syntax:

int sum(int a, int b)
String sum(int a, int b)

2. Operator Overloading

Operator overloading means giving multiple meanings to the same operator. Unlike C++, Java does not support user-defined operator overloading. However, similar behavior can be achieved using overloaded methods.

**Syntax:

void add(int a, int b)
void add(String a, String b)

Java `

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

**Explanation: The same add() method name performs different tasks. One method adds integers, while the other concatenates strings, achieving behavior similar to operator overloading.

Advantages of Compile-Time Polymorphism