Category: java
Polymorphism
Published on 22 Feb 2026
Explanation
Compile-time polymorphism is achieved
using method overloading.
Code:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
Explanation
Runtime polymorphism is achieved using
method overriding.
Code:
class A { void show() { System.out.println("A"); } }
class B extends A { void show() { System.out.println("B"); } }
Explanation
In compile-time polymorphism, method
resolution happens at compile time.
Code:
Calculator c = new Calculator(); c.add(10, 20);
Explanation
In runtime polymorphism, method call
depends on object type at runtime.
Code:
A obj = new B(); obj.show();
Explanation
Overloading requires different method signatures.
Code:
void print(int a) {}
void print(String a) {}