Compile-Time Polymorphism in Java

Last Updated : 9 Jan 2026

Compile-time polymorphism in Java allows the compiler to decide which method to call based on the method signature. In this chapter, you will learn how Java achieves this using method overloading with practical examples.

What is Compile-Time Polymorphism?

Compile-time polymorphism, also known as static polymorphism or early binding, is the ability of a programming language to determine which method or function to invoke at compile time based on the number, type, and order of parameters. The compile-time polymorphism is achieved through method overloading that allows multiple methods with the same name but different parameter lists to exist within the same class.

Syntax of Compile-Time Polymorphism

The following syntax shows how compile-time polymorphism is achieved using method overloading:

Examples of Compile-Time Polymorphism

Practice the following examples to understand how compile-time polymorphism works using method overloading.

Example 1: Method Overloading Using Different Data Types

The following example demonstrates compile-time polymorphism by overloading the add() method with different parameter data types.

Here's a complete Java code example that demonstrates compile-time polymorphism through method overloading:

Output:

Sum of 5 and 10 (integers): 15
Sum of 2.5 and 3.7 (doubles): 6.2

A Calculator class with two add methods can be found in the code above. The second method accepts two doubles as arguments and returns the sum as a double, in contrast to the first method's use of two integers and return of their sum as an integer.

In the main method, we construct a Calculator class instance and call the add methods with various arguments. The outcome is displayed on the console, indicating the appropriate compile-time selection of the overloaded methods depending on the argument types.

When you run the code, you ought to see the output that was previously indicated, which verifies that the correct add methods were called based on the parameter types provided during the method calls.

Example 2: Method Overloading Using Different Number of Parameters

The following example shows compile-time polymorphism by overloading a method with a different number of parameters.

Output:

Multiplication of 2 and 3: 6
Multiplication of 2, 3, and 4: 24

Here, the compiler determines which multiply() method to call based on the number of arguments provided, demonstrating compile-time polymorphism.

Compile-Time Polymorphism Using Constructor Overloading

Constructor overloading is a form of compile-time polymorphism where a class has multiple constructors with the same name but different parameter lists. The compiler decides which constructor to call based on the arguments provided at the time of object creation.

Example

The following example demonstrates compile-time polymorphism using constructor overloading.

Output:

101 Unknown
102 Rahul

Compile-Time Polymorphism Using Method Overloading with Type Promotion

Method overloading with type promotion occurs when the compiler automatically converts a smaller data type into a larger compatible data type to match a method’s parameter list. This decision is made at compile time.

Example

The following example demonstrates compile-time polymorphism using method overloading with type promotion:

Output:

Integer value: 10