Compile-time polymorphism lets a program decide which method to use before it runs. It helps make code simpler and easier to use.
Compile-time polymorphism in Java
Start learning this pattern below
Jump into concepts and practice - no test required
class ClassName { void methodName() { } void methodName(int a) { } void methodName(int a, int b) { } }
Methods have the same name but different parameters (number or type).
The compiler decides which method to call based on the arguments used.
add but with different numbers of inputs.class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }
print that accept different types of inputs.class Printer { void print(String s) { System.out.println(s); } void print(int n) { System.out.println(n); } }
This program shows three show methods with different parameters. The right one runs based on what we give.
class Demo { void show() { System.out.println("No parameters"); } void show(int a) { System.out.println("One parameter: " + a); } void show(int a, int b) { System.out.println("Two parameters: " + a + ", " + b); } public static void main(String[] args) { Demo obj = new Demo(); obj.show(); obj.show(5); obj.show(3, 7); } }
Compile-time polymorphism is also called method overloading.
The method signature (name + parameters) must be different for overloading.
Return type alone cannot create overloading.
Compile-time polymorphism means choosing the method to run before the program starts.
It uses the same method name but different parameters.
This makes code easier to read and reuse.
Practice
What is compile-time polymorphism in Java?
Solution
Step 1: Understand method overloading
Compile-time polymorphism is also called method overloading, where methods share the same name but differ in parameters.Step 2: Differentiate from runtime polymorphism
Runtime polymorphism uses method overriding, changing behavior based on object type at runtime, not compile-time.Final Answer:
Using the same method name with different parameters in the same class -> Option CQuick Check:
Compile-time polymorphism = method overloading [OK]
- Confusing compile-time with runtime polymorphism
- Thinking method overriding is compile-time polymorphism
- Believing different method names are polymorphism
Which of the following is the correct syntax for method overloading in Java?
public class Calculator {
public int add(int a, int b) { return a + b; }
public int add(int a, int b, int c) { ? }
}Solution
Step 1: Check method parameters
The second add method has three parameters, so it should add all three values.Step 2: Write correct return statement
Return the sum of a, b, and c to correctly overload the add method.Final Answer:
return a + b + c; -> Option AQuick Check:
Overloaded method sums all parameters [OK]
- Returning sum of only two parameters in three-parameter method
- Using wrong operators like multiplication or subtraction
- Syntax errors like missing semicolon
What will be the output of the following Java code?
class Demo {
void show(int a) { System.out.println("Int: " + a); }
void show(String a) { System.out.println("String: " + a); }
}
public class Test {
public static void main(String[] args) {
Demo d = new Demo();
d.show(5);
d.show("Hello");
}
}Solution
Step 1: Identify overloaded methods
There are two show methods: one takes int, the other takes String.Step 2: Match method calls to parameters
d.show(5) calls show(int), printing "Int: 5"; d.show("Hello") calls show(String), printing "String: Hello".Final Answer:
Int: 5\nString: Hello -> Option AQuick Check:
Method overloading calls correct method by parameter type [OK]
- Confusing parameter types and outputs
- Expecting runtime polymorphism behavior
- Thinking it causes compilation error
Find the error in this code snippet related to compile-time polymorphism:
class Test {
void display(int a) { System.out.println(a); }
void display(int a, int b) { System.out.println(a + b); }
void display(int a) { System.out.println(a * 2); }
}Solution
Step 1: Check method signatures
Two methods have the exact same name and parameter list: display(int a).Step 2: Understand method overloading rules
Method overloading requires different parameter lists; duplicate signatures cause compilation error.Final Answer:
Duplicate method display(int a) causes compilation error -> Option BQuick Check:
Duplicate method signatures cause compile error [OK]
- Thinking method bodies affect overloading
- Ignoring duplicate parameter lists
- Assuming code compiles without error
Consider this class:
class Printer {
void print(int a) { System.out.println("Number: " + a); }
void print(String a) { System.out.println("Text: " + a); }
void print(int a, String b) { System.out.println(a + " and " + b); }
}Which call will cause a compile-time error?
Solution
Step 1: Check method signatures
Methods accept (int), (String), and (int, String) parameters.Step 2: Match call parameters
print("Hello", 10) tries to call (String, int), which does not exist, causing compile error.Final Answer:
print("Hello", 10) -> Option DQuick Check:
No matching method for (String, int) call [OK]
- Assuming parameter order doesn't matter
- Thinking all combinations are allowed
- Ignoring method signature mismatch
