SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java Janu Jahnavi
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in javasureshraj43
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its typesSuraj Bora
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Kamlesh Makvana
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++Sujan Mia
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 

Was ist angesagt? (20)

Wrapper class
Wrapper classWrapper class
Wrapper class
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
OOP java
OOP javaOOP java
OOP java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its types
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Ähnlich wie Learn Polymorphism in Java: Types, Examples and Advantages

Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)Geekster
 
Learn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdfLearn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdfDatacademy.ai
 
polymorphism method overloading and overriding .pptx
polymorphism method overloading  and overriding .pptxpolymorphism method overloading  and overriding .pptx
polymorphism method overloading and overriding .pptxthamaraiselvangts441
 
Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overridingJavaTportal
 
Principles of Object Oriented Programming
Principles of Object Oriented ProgrammingPrinciples of Object Oriented Programming
Principles of Object Oriented ProgrammingKasun Ranga Wijeweera
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Learn java lessons_online
Learn java lessons_onlineLearn java lessons_online
Learn java lessons_onlinenishajj
 
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...ShuvrojitMajumder
 
Java Polymorphism
Java PolymorphismJava Polymorphism
Java PolymorphismSoba Arjun
 
Learn java objects inheritance-overriding-polymorphism
Learn java objects  inheritance-overriding-polymorphismLearn java objects  inheritance-overriding-polymorphism
Learn java objects inheritance-overriding-polymorphismTOPS Technologies
 
P.7 media 2 polymorphism
P.7 media 2 polymorphismP.7 media 2 polymorphism
P.7 media 2 polymorphismahmadmuzaqqi
 

Ähnlich wie Learn Polymorphism in Java: Types, Examples and Advantages (20)

Java
JavaJava
Java
 
Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)
 
Learn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdfLearn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdf
 
java poly ppt.pptx
java poly ppt.pptxjava poly ppt.pptx
java poly ppt.pptx
 
polymorphism method overloading and overriding .pptx
polymorphism method overloading  and overriding .pptxpolymorphism method overloading  and overriding .pptx
polymorphism method overloading and overriding .pptx
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overriding
 
Principles of Object Oriented Programming
Principles of Object Oriented ProgrammingPrinciples of Object Oriented Programming
Principles of Object Oriented Programming
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
Learn java lessons_online
Learn java lessons_onlineLearn java lessons_online
Learn java lessons_online
 
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
 
Java Polymorphism
Java PolymorphismJava Polymorphism
Java Polymorphism
 
Learn java objects inheritance-overriding-polymorphism
Learn java objects  inheritance-overriding-polymorphismLearn java objects  inheritance-overriding-polymorphism
Learn java objects inheritance-overriding-polymorphism
 
P.7 media 2 polymorphism
P.7 media 2 polymorphismP.7 media 2 polymorphism
P.7 media 2 polymorphism
 

Mehr von Spotle.ai

Spotle AI-thon - AI For Good Business Plan Showcase - Team IIM Indore - AI Ro...
Spotle AI-thon - AI For Good Business Plan Showcase - Team IIM Indore - AI Ro...Spotle AI-thon - AI For Good Business Plan Showcase - Team IIM Indore - AI Ro...
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-thon - AI For Good Business Plan Showcase - Cummins CollegeSpotle AI-thon - AI For Good Business Plan Showcase - Cummins College
Spotle AI-thon - AI For Good Business Plan Showcase - Cummins CollegeSpotle.ai
 
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Elit...
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Elit...Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Elit...
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-thon Top 10 Showcase - Analysing Mental Health Of India- Ankur chat...Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India- Ankur chat...
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-thon Top 10 Showcase - Analysing Mental Health Of India - Team La c...Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team La c...
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-thon Top 10 Showcase - Analysing Mental Health Of India - Team Temp...Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Temp...
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-thon Top 10 Showcase - Analysing Mental Health Of India - Team Zer... Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Zer...
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-thon Top 10 Showcase - Analysing Mental Health Of India - Shivam Gi...Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Shivam Gi...
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-thon Top 10 Showcase - Analysing Mental Health Of India - Cyber Pun...Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Cyber Pun...
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-thon Top 10 Showcase - Analysing Mental Health Of India - Tech Owls...Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Tech Owls...
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-thon Top 10 Showcase - Analysing Mental Health Of India - Team Jar... Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Jar...
Spotle AI-thon Top 10 Showcase - Analysing Mental Health Of India - Team Jar...Spotle.ai
 
Artificial intelligence in fintech
Artificial intelligence in fintechArtificial intelligence in fintech
Artificial intelligence in fintechSpotle.ai
 
Semi-supervised Machine Learning
Semi-supervised Machine LearningSemi-supervised Machine Learning
Semi-supervised Machine LearningSpotle.ai
 
Basics of Reinforcement Learning
Basics of Reinforcement LearningBasics of Reinforcement Learning
Basics of Reinforcement LearningSpotle.ai
 
Tableau And Data Visualization - Get Started
Tableau And Data Visualization - Get StartedTableau And Data Visualization - Get Started
Tableau And Data Visualization - Get StartedSpotle.ai
 
Artificial Intelligence in FinTech
Artificial Intelligence in FinTechArtificial Intelligence in FinTech
Artificial Intelligence in FinTechSpotle.ai
 
Supervised and Unsupervised Machine Learning
Supervised and Unsupervised Machine LearningSupervised and Unsupervised Machine Learning
Supervised and Unsupervised Machine LearningSpotle.ai
 
Growing-up With AI
Growing-up With AIGrowing-up With AI
Growing-up With AISpotle.ai
 
AI And Cyber-security Threats
AI And Cyber-security ThreatsAI And Cyber-security Threats
AI And Cyber-security ThreatsSpotle.ai
 
Robotic Process Automation With Blue Prism
Robotic Process Automation With Blue PrismRobotic Process Automation With Blue Prism
Robotic Process Automation With Blue PrismSpotle.ai
 

Mehr von Spotle.ai (20)

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

Kürzlich hochgeladen

Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 

Kürzlich hochgeladen (20)

Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 

Learn Polymorphism in Java: Types, Examples and Advantages

  • 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