[Go to site: main page, start]

0% found this document useful (0 votes)
3 views5 pages

Understanding Java Polymorphism Concepts

The document provides an overview of polymorphism in Java, defining it as the ability for one object to take many forms. It explains two types of polymorphism: compile-time (method overloading) where methods have the same name but different parameters, and run-time (method overriding) where a child class provides a specific implementation. Key examples illustrate how these concepts work in practice.

Uploaded by

Dev G
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Understanding Java Polymorphism Concepts

The document provides an overview of polymorphism in Java, defining it as the ability for one object to take many forms. It explains two types of polymorphism: compile-time (method overloading) where methods have the same name but different parameters, and run-time (method overriding) where a child class provides a specific implementation. Key examples illustrate how these concepts work in practice.

Uploaded by

Dev G
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like