Java Notes – Polymorphism
Prepared for Students • September 15, 2025
Overview
Today's Class Notes
Topic: Polymorphism in Java
Definition
Definition of Polymorphism:
- "Poly" means many, "Morph" means form.
- Polymorphism means one object can take many forms.
- In Java, polymorphism allows the same method name or variable to perform dif
Types of Polymorphism:
1. Compile-time Polymorphism (Method Overloading)
2. Run-time Polymorphism (Method Overriding)
Compile-time Polymorphism (Method Overloading)
1) Compile-time Polymorphism (Method Overloading)
Definition:
- Method overloading means having multiple methods with the same name but diff
- The method is selected at compile time.
Example: PizzaShop with different orderPizza methods.
Code Example
class PizzaShop {
void orderPizza() {
[Link]("You ordered a regular pizza");
}
void orderPizza(String topping) {
[Link]("You ordered pizza with " + topping);
}
void orderPizza(String topping, int extraCheese) {
[Link]("You ordered pizza with " + topping + " and " + ext
}
}
public class Main {
public static void main(String[] args) {
PizzaShop shop = new PizzaShop();
[Link]();
[Link]("mushrooms");
[Link]("olives", 2);
}
}
Output
Output:
You ordered a regular pizza
You ordered pizza with mushrooms
You ordered pizza with olives and 2x cheese
Explanation:
- All methods have the same name 'orderPizza' but different arguments.
- The correct version is chosen at compile time based on the arguments provide
Run-time Polymorphism (Method Overriding)
2) Run-time Polymorphism (Method Overriding)
Definition:
- Method overriding means a child class provides a specific implementation of
- The method call is resolved at runtime based on the object type.
Example: Different singers overriding the sing() method.
Code Example
class Singing {
void sing() {
[Link]("Beautifully singing");
}
}
class KishorKumar extends Singing {
@Override
void sing() {
[Link]("The legend Kishor Kumar is singing now");
}
}
class ArijitSingh extends Singing {
@Override
void sing() {
[Link]("The legend Arijit Singh is singing now");
}
}
public class MethodOverriding {
public static void main(String[] args) {
Singing s1 = new KishorKumar();
Singing s2 = new ArijitSingh();
[Link]();
[Link]();
}
}
Output
Output:
The legend Kishor Kumar is singing now
The legend Arijit Singh is singing now
Explanation:
- Both child classes override the sing() method in their own way.
- Even though the reference type is 'Singing', the actual object type decides
Summary
Key Takeaways:
- Polymorphism = One name, many forms.
- Method Overloading = Same method name, different parameters (compile time).
- Method Overriding = Same method name and parameters, but different implement