SlideShare a Scribd company logo
Polymorphism in Java
Understanding Compile-time and
Runtime Polymorphism with
Examples
What is Polymorphism?
• Polymorphism allows one interface to be used
for different data types or actions.
• In Java, it helps in achieving flexibility and
reusability of code.
Types of Polymorphism in Java
• 1. Compile-time Polymorphism (Method
Overloading)
• 2. Runtime Polymorphism (Method
Overriding)
Compile-time Polymorphism
(Method Overloading)
• • Achieved using method overloading.
• • Methods have the same name but different
parameters.
• • The method to be executed is determined at
compile time.
Example: Method Overloading in
Java
class OverloadExample {
void display(int a) {
System.out.println("Integer: " + a);
}
void display(String a) {
System.out.println("String: " + a);
}
public static void main(String args[]) {
OverloadExample obj = new OverloadExample();
obj.display(10);
obj.display("Java");
}
}
Runtime Polymorphism (Method
Overriding)
• • Achieved using method overriding.
• • A subclass provides a specific
implementation of a method already defined
in its parent class.
• • The method to be executed is determined at
runtime.
Example: Method Overriding in
Java
class Parent {
void show() {
System.out.println("Parent class method");
}
}
class Child extends Parent {
void show() {
System.out.println("Child class method");
}
public static void main(String args[]) {
Parent obj = new Child();
obj.show();
}
}
Key Differences: Overloading vs
Overriding
• • Overloading happens at compile time,
Overriding happens at runtime.
• • Overloading is within the same class,
Overriding involves inheritance.
• • Overloaded methods have different
parameters, Overridden methods have the
same signature.
Summary
• • Polymorphism allows flexibility and
reusability in code.
• • Two types: Compile-time (Method
Overloading) and Runtime (Method
Overriding).
• • Helps in writing cleaner and scalable
programs.
Thank You!
• Any Questions?

More Related Content

PPTX
Java(Polymorphism)
PPTX
Polymorphism
PPTX
Polymorphism in java
PPTX
java poly ppt.pptx
PPTX
Polymorphism in java
PPT
Polymorphism in java
PDF
Polymorphism in Java by Animesh Sarkar
PDF
Java Polymorphism: Types And Examples (Geekster)
Java(Polymorphism)
Polymorphism
Polymorphism in java
java poly ppt.pptx
Polymorphism in java
Polymorphism in java
Polymorphism in Java by Animesh Sarkar
Java Polymorphism: Types And Examples (Geekster)

Similar to Basics of polymorphism underlying hhhhhhh (20)

PPTX
P.7 media 2 polymorphism
PDF
Object Oriented Programming - 7.2. Polymorphism
PPTX
OOP- PolymorphismFinal12injavait101.pptx
PPTX
Chapter8:Understanding Polymorphism
PPTX
Presentstion polymorphism opp Java muet u
PPTX
JAVA_POLYMORPHISM.pptx
PPTX
Polymorphism
PPTX
Chapter 4
PDF
Learn java lessons_online
PDF
Java Polymorphism
PPT
Polymorphism in java, method overloading and method overriding
PPTX
java polymorphism example program oracal.pptx
PPT
Introduction to method overloading & method overriding in java hdm
PPTX
Polymorphism.pptx
PPTX
Polymorphism OOP new Gate.pptx
PPTX
Polymorphism OOP Old Gate.pptx
PDF
Polymorphism In Java
PPTX
Advance Java Polymorphism OOPS CON .pptx
PDF
Object-Oriented Polymorphism Unleashed
PPT
Polymorphism
P.7 media 2 polymorphism
Object Oriented Programming - 7.2. Polymorphism
OOP- PolymorphismFinal12injavait101.pptx
Chapter8:Understanding Polymorphism
Presentstion polymorphism opp Java muet u
JAVA_POLYMORPHISM.pptx
Polymorphism
Chapter 4
Learn java lessons_online
Java Polymorphism
Polymorphism in java, method overloading and method overriding
java polymorphism example program oracal.pptx
Introduction to method overloading & method overriding in java hdm
Polymorphism.pptx
Polymorphism OOP new Gate.pptx
Polymorphism OOP Old Gate.pptx
Polymorphism In Java
Advance Java Polymorphism OOPS CON .pptx
Object-Oriented Polymorphism Unleashed
Polymorphism
Ad

Recently uploaded (20)

PPTX
Zeem: Transition Your Fleet, Seamlessly by Margaret Boelter
PDF
Volvo EC20C Excavator Service maintenance schedules.pdf
PDF
Marketing project 2024 for marketing students
PPTX
Fire Fighting Unit IV industrial safety.pptx
PDF
industrial engineering and safety system
PPTX
capstoneoooooooooooooooooooooooooooooooooo
PDF
3-REasdfghjkl;[poiunvnvncncn-Process.pdf
PDF
Honda Dealership SNS Evaluation pdf/ppts
PPTX
Lecture 3b C Library xnxjxjxjxkx_ ESP32.pptx
PDF
Volvo EC300D L EC300DL excavator weight Manuals.pdf
PDF
Journal Meraj.pdfuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
DOCX
lp of food hygiene.docxvvvvvvvvvvvvvvvvvvvvvvv
PDF
RPL-ASDC PPT PROGRAM NSDC GOVT SKILLS INDIA
PPTX
Gayatri Cultural Educational Society.pptx
PPTX
IMMUNITY TYPES PPT.pptx very good , sufficient
PDF
How Much does a Volvo EC290C NL EC290CNL Weight.pdf
PDF
Volvo EC20C Excavator Step-by-step Maintenance Instructions pdf
PDF
Caterpillar CAT 312B L EXCAVATOR (2KW00001-UP) Operation and Maintenance Manu...
PPTX
Culture by Design.pptxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PDF
Caterpillar Cat 315C Excavator (Prefix ANF) Service Repair Manual Instant Dow...
Zeem: Transition Your Fleet, Seamlessly by Margaret Boelter
Volvo EC20C Excavator Service maintenance schedules.pdf
Marketing project 2024 for marketing students
Fire Fighting Unit IV industrial safety.pptx
industrial engineering and safety system
capstoneoooooooooooooooooooooooooooooooooo
3-REasdfghjkl;[poiunvnvncncn-Process.pdf
Honda Dealership SNS Evaluation pdf/ppts
Lecture 3b C Library xnxjxjxjxkx_ ESP32.pptx
Volvo EC300D L EC300DL excavator weight Manuals.pdf
Journal Meraj.pdfuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
lp of food hygiene.docxvvvvvvvvvvvvvvvvvvvvvvv
RPL-ASDC PPT PROGRAM NSDC GOVT SKILLS INDIA
Gayatri Cultural Educational Society.pptx
IMMUNITY TYPES PPT.pptx very good , sufficient
How Much does a Volvo EC290C NL EC290CNL Weight.pdf
Volvo EC20C Excavator Step-by-step Maintenance Instructions pdf
Caterpillar CAT 312B L EXCAVATOR (2KW00001-UP) Operation and Maintenance Manu...
Culture by Design.pptxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Caterpillar Cat 315C Excavator (Prefix ANF) Service Repair Manual Instant Dow...
Ad

Basics of polymorphism underlying hhhhhhh

  • 1. Polymorphism in Java Understanding Compile-time and Runtime Polymorphism with Examples
  • 2. What is Polymorphism? • Polymorphism allows one interface to be used for different data types or actions. • In Java, it helps in achieving flexibility and reusability of code.
  • 3. Types of Polymorphism in Java • 1. Compile-time Polymorphism (Method Overloading) • 2. Runtime Polymorphism (Method Overriding)
  • 4. Compile-time Polymorphism (Method Overloading) • • Achieved using method overloading. • • Methods have the same name but different parameters. • • The method to be executed is determined at compile time.
  • 5. Example: Method Overloading in Java class OverloadExample { void display(int a) { System.out.println("Integer: " + a); } void display(String a) { System.out.println("String: " + a); } public static void main(String args[]) { OverloadExample obj = new OverloadExample(); obj.display(10); obj.display("Java"); } }
  • 6. Runtime Polymorphism (Method Overriding) • • Achieved using method overriding. • • A subclass provides a specific implementation of a method already defined in its parent class. • • The method to be executed is determined at runtime.
  • 7. Example: Method Overriding in Java class Parent { void show() { System.out.println("Parent class method"); } } class Child extends Parent { void show() { System.out.println("Child class method"); } public static void main(String args[]) { Parent obj = new Child(); obj.show(); } }
  • 8. Key Differences: Overloading vs Overriding • • Overloading happens at compile time, Overriding happens at runtime. • • Overloading is within the same class, Overriding involves inheritance. • • Overloaded methods have different parameters, Overridden methods have the same signature.
  • 9. Summary • • Polymorphism allows flexibility and reusability in code. • • Two types: Compile-time (Method Overloading) and Runtime (Method Overriding). • • Helps in writing cleaner and scalable programs.
  • 10. Thank You! • Any Questions?