0
0
Javaprogramming~10 mins

Compile-time polymorphism in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Compile-time polymorphism
Start
Call method with arguments
Compiler checks method signatures
Select method matching argument types
Execute selected method
End
At compile time, Java decides which method to call based on the method name and argument types.
Execution Sample
Java
class Demo {
  void show(int a) { System.out.println("int: " + a); }
  void show(String a) { System.out.println("String: " + a); }
  public static void main(String[] args) {
    Demo d = new Demo();
    d.show(5);
    d.show("Hi");
  }
}
This code shows two methods named show with different argument types; Java chooses which to call based on the argument.
Execution Table
StepMethod CallArgument TypeMethod SelectedOutput
1d.show(5)intshow(int a)int: 5
2d.show("Hi")Stringshow(String a)String: Hi
3End of main---
💡 All method calls resolved at compile time by matching argument types.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
dnullDemo objectDemo objectDemo object
a (int)N/A5N/AN/A
a (String)N/AN/A"Hi"N/A
Key Moments - 2 Insights
Why does Java choose which show method to call before running the program?
Because Java uses compile-time polymorphism, it decides which method to call by looking at the argument types during compilation, as shown in execution_table steps 1 and 2.
What happens if you call show with a type that doesn't match any method?
The compiler will give an error because it cannot find a matching method signature, so no method is selected.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which method is called when d.show(5) is executed?
Ashow(int a)
Bshow(String a)
Cshow(double a)
DNo method is called
💡 Hint
Check the first row of the execution_table where argument type is int.
At which step does the method show(String a) get called?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the second row of the execution_table for method selection.
If we add a method show(double a), what happens when calling d.show(5.0)?
Ashow(int a) is called
Bshow(String a) is called
Cshow(double a) is called
DCompilation error
💡 Hint
Compile-time polymorphism selects method matching argument type exactly.
Concept Snapshot
Compile-time polymorphism (method overloading) means multiple methods have the same name but different parameters.
Java decides which method to call during compilation by matching argument types.
This allows methods to behave differently based on input types.
If no matching method is found, compilation fails.
Example: show(int) and show(String) are two overloaded methods.
Full Transcript
Compile-time polymorphism in Java means the program decides which method to call before running, based on the method name and argument types. In the example, the class Demo has two methods named show: one takes an int and the other a String. When main calls d.show(5), Java chooses show(int a) because the argument is an int. When d.show("Hi") is called, Java chooses show(String a). This decision happens during compilation, so the program knows exactly which method to run. If you try to call show with a type that doesn't match any method, the compiler will give an error. This is called method overloading and helps write clear code that works with different data types.