SlideShare a Scribd company logo
4
Most read
6
Most read
13
Most read
Spotle.ai Study Material
Spotle.ai/Learn
Polymorphism
in Java
Spotle.ai Study Material
Spotle.ai/Learn
Polymorphism is the concept of
one entity providing multiple
implementations or behaviours.
(Something like an Avatar!)
What Is Polymorphism?
2
Spotle.ai Study Material
Spotle.ai/Learn
3
Peter Parker Is Polymorphic
As a high school
student, Peter
goes to school
and hangs out
with friends. As
Spider Man, he
bashes up
baddies. Peter
has multiple
behaviours
based on the
context. Peter is
polymorphic.
Spotle.ai Study Material
Spotle.ai/Learn
Java provides
multiple forms of
polymorphism. It
allows you to define
the same method
name to do multiple
different things. It
also allows child
classes to redefine/
override parents’
behaviours for the
same method.
Java And Polymorphism
4
Spotle.ai Study Material
Spotle.ai/Learn
Java Provides 2 Types Of Polymorphism
5
Compile -Time
Polymorphism
Run-Time
Polymorphism
Polymorphism In
Java
Spotle.ai Study Material
Spotle.ai/Learn
Compile-time polymorphism refers to
behaviour that is resolved when your
Java class is compiled. Method
overloading is an example of
compile-time polymorphism. Method
overloading is how you can use
method with same name to do
different things based on the
parameters passed.
The add method in Class Calculator
for example can add 2 integers or
floats based on the types of
parameters.
Compile-Time Or Static Polymorphism
A calculator can add 2 integers. It can also add 2 floats.
The addition method adds differently based on the inputs.
6
Spotle.ai Study Material
Spotle.ai/Learn
Illustration – Method Overloading
7
Public class PolymorphicCalculator
{
//Add method to add integers
int add (int a, int b)
{
System.out.println(“Adding 2 Integers”);
return a+b;
}
//Another Add method to add floats. Same name //
differentiated by data types of parameters and //
return types
float add (float a, float b)
{
System.out.println(“Adding 2 Floats”);
return a+b;
}
}
The Class
PolymorphicCalculator
provides multiple
implementation of the
method add.
Spotle.ai Study Material
Spotle.ai/Learn
Illustration – Method Overloading
8
Adding a main method to
Polymorphic Calculator. The
compiler chooses the correct
add method to call based on
the data types of actual
arguments passed.
Public static void main(String args[])
{
PolymorphicCalculator calc = new
PolymorphicCalculator ();
//calling the add method with integer
arguments
System.out.println(calc.add (3,5));
//calling the add method with float arguments
System.out.println(calc.add(3.5,5.6));
}
}
The output will be:
Adding 2 Integers
8
Adding 2 Floats
9.1
Spotle.ai Study Material
Spotle.ai/Learn
Run-time polymorphism refers to
behaviour that is resolved when your
Java class is run by the JVM. Method
overriding by the sub-class is an
example of run-time polymorphism.
Method overriding allows child
classes to provide their own
implementation of a method also
defined in the parent class.
The JVM decides which version of
the method (the child’s or the
parent’s) to call based on the object
through which the method is invoked.
Run-Time Or Dynamic Polymorphism
Consider a SeniorCitizenAccount class. It
will provide a calculateInterest method()
that overrides the calculateInterest()
method in the parent SavingsAccount class
9
Spotle.ai Study Material
Spotle.ai/Learn
Public Class SavingsAccount
{
float interest;
float FixedDeposit;
//Constructor
SavingsAccount(float interest, float
FixedDeposit)
{this.seniorInterest = interest;
this.fixedDeposit = fixedDeposit;
}
//Calculate interest of parent class method
float calculateInterest()
{
System.out.println(“Calculating Savings Account
Interest.”);
return (FixedDeposit*interest/100);
}
}
Defining The Base Or Parent Class Method
10
Spotle.ai Study Material
Spotle.ai/Learn
Public Class SeniorAccount extends
SavingsAccount
{
float seniorInterest;
//constructor
SavingsAccount(float interest, float FixedDeposit)
{
this.seniorInterest = interest;
this.fixedDeposit = fixedDeposit;
}
//Overriding calculateInterest() method in parent
class
float calculateInterest()
{
System.out.println(“Calculating Senior Account
Interest.”);
return (FixedDeposit*seniorInterest/100);
}
}
Defining The Overriding Method
11
Spotle.ai Study Material
Spotle.ai/Learn
Public static void main (String args[])
{
SavingsAccount saving = new
SavingsAccoun(6,100000);
//This will call calculateInterest() of parent
class
System.out.println(saving.calculateInterest());
SeniorAccount senior = new
SeniorAccoun(10,100000);
//This will call calculateInterest() of parent
class
System.out.println(senior.s.calculateInterest()
);
}
Illustrating Run Time Polymorphism
12
The output is:
Calculating Savings Account Interest.
6000.0
Calculating Senior Account Interest.
10000.0
Spotle.ai Study Material
Spotle.ai/Learn
Advantages Of Polymorphism
13
Code
Cleanliness
Ease Of
Implementation
Aligned With
Real World
Overloaded
Constructors
Reusability And
Extensibility

More Related Content

PPTX
Java(Polymorphism)
harsh kothari
 
PDF
Polymorphism in Java
Java2Blog
 
PPTX
Polymorphism presentation in java
Ahsan Raja
 
PPTX
Collision Theory
SPCGC AJMER
 
PPSX
Class as the basis of all computation
abhijeetkumarkar422
 
PPTX
A basic PPT on Internet Of Things(IOT)
jaswinder singh thind
 
PPTX
WHAT IS ABSTRACTION IN JAVA
sivasundari6
 
PPTX
Cryptocurrency; The future of Money?
Samrand Hassan
 
Java(Polymorphism)
harsh kothari
 
Polymorphism in Java
Java2Blog
 
Polymorphism presentation in java
Ahsan Raja
 
Collision Theory
SPCGC AJMER
 
Class as the basis of all computation
abhijeetkumarkar422
 
A basic PPT on Internet Of Things(IOT)
jaswinder singh thind
 
WHAT IS ABSTRACTION IN JAVA
sivasundari6
 
Cryptocurrency; The future of Money?
Samrand Hassan
 

What's hot (20)

PDF
Arrays in Java
Naz Abdalla
 
PPTX
This keyword in java
Hitesh Kumar
 
PPTX
Super keyword in java
Hitesh Kumar
 
PPTX
Polymorphism in java
Elizabeth alexander
 
PPTX
Control statements in java
Madishetty Prathibha
 
PPTX
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
PPTX
Arrays in Java
Abhilash Nair
 
PPTX
Inner classes in java
PhD Research Scholar
 
PPT
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
PPTX
INHERITANCE IN JAVA.pptx
NITHISG1
 
PPTX
Java program structure
shalinikarunakaran1
 
PPTX
OOPS Basics With Example
Thooyavan Venkatachalam
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
Operators in java
Then Murugeshwari
 
PPTX
Constructor in java
Madishetty Prathibha
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPTX
Methods in java
chauhankapil
 
PPTX
this keyword in Java.pptx
ParvizMirzayev2
 
PPT
9. Input Output in java
Nilesh Dalvi
 
Arrays in Java
Naz Abdalla
 
This keyword in java
Hitesh Kumar
 
Super keyword in java
Hitesh Kumar
 
Polymorphism in java
Elizabeth alexander
 
Control statements in java
Madishetty Prathibha
 
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Arrays in Java
Abhilash Nair
 
Inner classes in java
PhD Research Scholar
 
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
INHERITANCE IN JAVA.pptx
NITHISG1
 
Java program structure
shalinikarunakaran1
 
OOPS Basics With Example
Thooyavan Venkatachalam
 
Type casting in java
Farooq Baloch
 
Operators in java
Then Murugeshwari
 
Constructor in java
Madishetty Prathibha
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Inheritance in c++
Vineeta Garg
 
Methods in java
chauhankapil
 
this keyword in Java.pptx
ParvizMirzayev2
 
9. Input Output in java
Nilesh Dalvi
 
Ad

Similar to Polymorphism In Java (20)

PDF
Java Polymorphism: Types And Examples (Geekster)
Geekster
 
PPTX
OOP- PolymorphismFinal12injavait101.pptx
codevincent624
 
PDF
Learn Polymorphism in Python with Examples.pdf
Datacademy.ai
 
PPTX
java poly ppt.pptx
sukhpreetsingh295239
 
PPTX
polymorphism method overloading and overriding .pptx
thamaraiselvangts441
 
PDF
Polymorphism
Raffaele Doti
 
PPTX
Chapter 4
siragezeynu
 
PPT
Polymorphism in java, method overloading and method overriding
JavaTportal
 
PPTX
Chapter 04 Object Oriented programming .pptx
fikadumeuedu
 
PDF
Principles of Object Oriented Programming
Kasun Ranga Wijeweera
 
PPTX
Top 10 java oops interview questions
nishajj
 
PDF
Top 10 java_oops_interview_questions
nishajj
 
PDF
Top 10 java_oops_interview_questions
nishajj
 
PDF
Top 10 java_oops_interview_questions
nishajj
 
PPTX
Top 10 java oops interview questions
nishajj
 
PDF
Top 10 java_oops_interview_questions
nishajj
 
PDF
Week 8,9,10 of lab of data communication .pdf
nimrastorage123
 
PPTX
Polymorphism.pptx
RiturajJain8
 
PDF
Learn java lessons_online
nishajj
 
Java Polymorphism: Types And Examples (Geekster)
Geekster
 
OOP- PolymorphismFinal12injavait101.pptx
codevincent624
 
Learn Polymorphism in Python with Examples.pdf
Datacademy.ai
 
java poly ppt.pptx
sukhpreetsingh295239
 
polymorphism method overloading and overriding .pptx
thamaraiselvangts441
 
Polymorphism
Raffaele Doti
 
Chapter 4
siragezeynu
 
Polymorphism in java, method overloading and method overriding
JavaTportal
 
Chapter 04 Object Oriented programming .pptx
fikadumeuedu
 
Principles of Object Oriented Programming
Kasun Ranga Wijeweera
 
Top 10 java oops interview questions
nishajj
 
Top 10 java_oops_interview_questions
nishajj
 
Top 10 java_oops_interview_questions
nishajj
 
Top 10 java_oops_interview_questions
nishajj
 
Top 10 java oops interview questions
nishajj
 
Top 10 java_oops_interview_questions
nishajj
 
Week 8,9,10 of lab of data communication .pdf
nimrastorage123
 
Polymorphism.pptx
RiturajJain8
 
Learn java lessons_online
nishajj
 
Ad

More from Spotle.ai (20)

PDF
Spotle AI-thon - AI For Good Business Plan Showcase - Team IIM Indore - AI Ro...
Spotle.ai
 
PDF
Spotle AI-thon - AI For Good Business Plan Showcase - Cummins College
Spotle.ai
 
PDF
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Elit...
Spotle.ai
 
PDF
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India- Ankur chat...
Spotle.ai
 
PDF
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team La c...
Spotle.ai
 
PDF
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Temp...
Spotle.ai
 
PDF
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Zer...
Spotle.ai
 
PDF
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Shivam Gi...
Spotle.ai
 
PDF
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Cyber Pun...
Spotle.ai
 
PDF
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Tech Owls...
Spotle.ai
 
PDF
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Jar...
Spotle.ai
 
PDF
Artificial intelligence in fintech
Spotle.ai
 
PDF
Semi-supervised Machine Learning
Spotle.ai
 
PDF
Basics of Reinforcement Learning
Spotle.ai
 
PDF
Tableau And Data Visualization - Get Started
Spotle.ai
 
PDF
Artificial Intelligence in FinTech
Spotle.ai
 
PDF
Supervised and Unsupervised Machine Learning
Spotle.ai
 
PDF
Growing-up With AI
Spotle.ai
 
PDF
AI And Cyber-security Threats
Spotle.ai
 
PDF
Robotic Process Automation With Blue Prism
Spotle.ai
 
Spotle AI-thon - AI For Good Business Plan Showcase - Team IIM Indore - AI Ro...
Spotle.ai
 
Spotle AI-thon - AI For Good Business Plan Showcase - Cummins College
Spotle.ai
 
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Elit...
Spotle.ai
 
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India- Ankur chat...
Spotle.ai
 
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team La c...
Spotle.ai
 
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Temp...
Spotle.ai
 
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Zer...
Spotle.ai
 
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Shivam Gi...
Spotle.ai
 
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Cyber Pun...
Spotle.ai
 
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Tech Owls...
Spotle.ai
 
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Jar...
Spotle.ai
 
Artificial intelligence in fintech
Spotle.ai
 
Semi-supervised Machine Learning
Spotle.ai
 
Basics of Reinforcement Learning
Spotle.ai
 
Tableau And Data Visualization - Get Started
Spotle.ai
 
Artificial Intelligence in FinTech
Spotle.ai
 
Supervised and Unsupervised Machine Learning
Spotle.ai
 
Growing-up With AI
Spotle.ai
 
AI And Cyber-security Threats
Spotle.ai
 
Robotic Process Automation With Blue Prism
Spotle.ai
 

Recently uploaded (20)

PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PDF
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Presentation about variables and constant.pptx
kr2589474
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
oapresentation.pptx
mehatdhavalrajubhai
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 

Polymorphism In Java

  • 2. Spotle.ai Study Material Spotle.ai/Learn Polymorphism is the concept of one entity providing multiple implementations or behaviours. (Something like an Avatar!) What Is Polymorphism? 2
  • 3. Spotle.ai Study Material Spotle.ai/Learn 3 Peter Parker Is Polymorphic As a high school student, Peter goes to school and hangs out with friends. As Spider Man, he bashes up baddies. Peter has multiple behaviours based on the context. Peter is polymorphic.
  • 4. Spotle.ai Study Material Spotle.ai/Learn Java provides multiple forms of polymorphism. It allows you to define the same method name to do multiple different things. It also allows child classes to redefine/ override parents’ behaviours for the same method. Java And Polymorphism 4
  • 5. Spotle.ai Study Material Spotle.ai/Learn Java Provides 2 Types Of Polymorphism 5 Compile -Time Polymorphism Run-Time Polymorphism Polymorphism In Java
  • 6. Spotle.ai Study Material Spotle.ai/Learn Compile-time polymorphism refers to behaviour that is resolved when your Java class is compiled. Method overloading is an example of compile-time polymorphism. Method overloading is how you can use method with same name to do different things based on the parameters passed. The add method in Class Calculator for example can add 2 integers or floats based on the types of parameters. Compile-Time Or Static Polymorphism A calculator can add 2 integers. It can also add 2 floats. The addition method adds differently based on the inputs. 6
  • 7. Spotle.ai Study Material Spotle.ai/Learn Illustration – Method Overloading 7 Public class PolymorphicCalculator { //Add method to add integers int add (int a, int b) { System.out.println(“Adding 2 Integers”); return a+b; } //Another Add method to add floats. Same name // differentiated by data types of parameters and // return types float add (float a, float b) { System.out.println(“Adding 2 Floats”); return a+b; } } The Class PolymorphicCalculator provides multiple implementation of the method add.
  • 8. Spotle.ai Study Material Spotle.ai/Learn Illustration – Method Overloading 8 Adding a main method to Polymorphic Calculator. The compiler chooses the correct add method to call based on the data types of actual arguments passed. Public static void main(String args[]) { PolymorphicCalculator calc = new PolymorphicCalculator (); //calling the add method with integer arguments System.out.println(calc.add (3,5)); //calling the add method with float arguments System.out.println(calc.add(3.5,5.6)); } } The output will be: Adding 2 Integers 8 Adding 2 Floats 9.1
  • 9. Spotle.ai Study Material Spotle.ai/Learn Run-time polymorphism refers to behaviour that is resolved when your Java class is run by the JVM. Method overriding by the sub-class is an example of run-time polymorphism. Method overriding allows child classes to provide their own implementation of a method also defined in the parent class. The JVM decides which version of the method (the child’s or the parent’s) to call based on the object through which the method is invoked. Run-Time Or Dynamic Polymorphism Consider a SeniorCitizenAccount class. It will provide a calculateInterest method() that overrides the calculateInterest() method in the parent SavingsAccount class 9
  • 10. Spotle.ai Study Material Spotle.ai/Learn Public Class SavingsAccount { float interest; float FixedDeposit; //Constructor SavingsAccount(float interest, float FixedDeposit) {this.seniorInterest = interest; this.fixedDeposit = fixedDeposit; } //Calculate interest of parent class method float calculateInterest() { System.out.println(“Calculating Savings Account Interest.”); return (FixedDeposit*interest/100); } } Defining The Base Or Parent Class Method 10
  • 11. Spotle.ai Study Material Spotle.ai/Learn Public Class SeniorAccount extends SavingsAccount { float seniorInterest; //constructor SavingsAccount(float interest, float FixedDeposit) { this.seniorInterest = interest; this.fixedDeposit = fixedDeposit; } //Overriding calculateInterest() method in parent class float calculateInterest() { System.out.println(“Calculating Senior Account Interest.”); return (FixedDeposit*seniorInterest/100); } } Defining The Overriding Method 11
  • 12. Spotle.ai Study Material Spotle.ai/Learn Public static void main (String args[]) { SavingsAccount saving = new SavingsAccoun(6,100000); //This will call calculateInterest() of parent class System.out.println(saving.calculateInterest()); SeniorAccount senior = new SeniorAccoun(10,100000); //This will call calculateInterest() of parent class System.out.println(senior.s.calculateInterest() ); } Illustrating Run Time Polymorphism 12 The output is: Calculating Savings Account Interest. 6000.0 Calculating Senior Account Interest. 10000.0
  • 13. Spotle.ai Study Material Spotle.ai/Learn Advantages Of Polymorphism 13 Code Cleanliness Ease Of Implementation Aligned With Real World Overloaded Constructors Reusability And Extensibility