Java Compile Time vs Runtime Polymorphism
Understand Java compile-time polymorphism via method overloading, runtime polymorphism via method overriding, and the practical differences in binding and API design.

In Java, polymorphism appears in two distinct forms: compile-time polymorphism, achieved through method overloading, and runtime polymorphism, achieved through method overriding. The difference determines which method is selected and when that selection happens. This article explains the mechanics, the binding differences, and how to choose between them in real code.
What Compile-Time Polymorphism Means in Java
Compile-time polymorphism in Java is realized through method overloading. Overloading allows a class to have multiple methods with the same name but different parameter lists. The compiler decides which method to call based on the number, types, and order of arguments at compile time. This is also known as static binding because the method resolution is fixed when the code is compiled.
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
}When you call calc.add(2, 3), the compiler picks the first method. If you pass 2.0, 3.0, it picks the second. This resolution happens entirely during compilation; the generated bytecode contains a direct reference to the chosen method. There is no runtime decision involved.
How Runtime Polymorphism Works
Runtime polymorphism is achieved through method overriding. A subclass provides its own implementation of a method inherited from a superclass. The method name and parameter list must match the superclass method; the return type can be the same or a covariant subtype. At runtime, the JVM determines which implementation to execute based on the actual object type, not the reference type. This is called dynamic binding or late binding.
class Animal {
public void sound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Bark");
}
}
Animal a = new Dog();
a.sound(); // prints "Bark"Here, the reference variable a is of type Animal, but the object it points to is a Dog. When sound() is invoked, the JVM looks up the actual class of the object and calls Dog.sound(). This decision is made at runtime, enabling polymorphic behavior where the same reference type can behave differently depending on the concrete object.
The Binding Difference: Static vs Dynamic Dispatch
The core distinction between compile-time and runtime polymorphism is when the method call is bound to a specific method implementation.
Static binding (compile-time) uses the declared type of the reference and the argument types to resolve the method. It is efficient because the compiler can emit a direct invocation. Overloaded methods are resolved this way.
Dynamic binding (runtime) uses the actual object type to resolve the method. A common implementation technique is a virtual method table (vtable) that maps method signatures to their concrete implementations. When a virtual method is called, the JVM dispatches through the vtable entry for the object's class. This adds a small overhead compared to static binding, but it enables inheritance and interface-based polymorphism.
Performance and Runtime Cost of Dynamic Dispatch
Dynamic dispatch is not free, but modern JVMs reduce the overhead. A virtual call usually requires an extra indirection through the vtable, while a statically bound call can be emitted directly. JIT compilers also use inline caching or devirtualization when they can determine the concrete type at runtime. For most applications, the performance difference is negligible; it matters mainly in hot code where many polymorphic call sites cannot be optimized.
If you are designing a high-frequency library and need to minimize dispatch overhead, examine the hot call sites first. In normal business logic, runtime polymorphism is the standard tool for extensibility and should not be avoided for micro-optimization reasons.
Practical Implications for API Design
Choosing between overloading and overriding is not just a syntactic preference. It affects how your API can evolve and how clients use it.
Overloading is useful when you want to provide multiple ways to invoke the same operation with different input types or arities. It is resolved at compile time, so the caller must know the exact types at compile time. Overriding is essential when you want to allow subclasses to extend or replace behavior. It is resolved at runtime, so the caller only needs a reference to the superclass or interface.
A common pattern is to combine both: a base class defines an overridable method, and overloaded convenience methods delegate to it. For example:
public class FileParser {
public void parse(String path) {
parse(new File(path));
}
public void parse(File file) {
// default implementation
}
}Subclasses can override parse(File) to change behavior, while callers can still use the parse(String) overload. This gives flexibility without breaking the API.
Common Pitfalls and Edge Cases
One subtle issue is that overloading is resolved at compile time, not runtime. If you have a method that accepts a superclass and another that accepts a subclass, the compiler picks the most specific method based on the declared type of the argument, not the actual object type.
void print(Object obj) { ... }
void print(String str) { ... }
Object o = "hello";
print(o); // calls print(Object), not print(String)This surprises many developers. To get runtime polymorphism, you must use overriding, not overloading.
Another edge case involves variable hiding. Fields are not polymorphic; they are resolved based on the reference type. If a subclass declares a field with the same name as a superclass field, the reference type determines which field is accessed. This is unrelated to method polymorphism but can cause confusion.
Choosing the Right Polymorphism for Your Code
Use compile-time polymorphism (overloading) when:
- The set of parameter types is known and fixed at compile time.
- You want to provide convenience methods without affecting subclass behavior.
- You need to support different argument combinations that do not require runtime type checks.
Use runtime polymorphism (overriding) when:
- You need to allow subclasses to provide their own implementation of a common operation.
- You are designing a framework where the caller only knows the interface or abstract class.
- You want to apply the Open/Closed Principle: open for extension, closed for modification.
In many real-world classes, both are used together. The key is to understand that overloading is a compile-time mechanism that does not participate in dynamic dispatch, while overriding is the foundation of Java's polymorphic behavior. By choosing the right mechanism for each method, you can build APIs that are both flexible and predictable.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.