SlideShare a Scribd company logo
1 of 20
Polymorphism
Object Oriented Programming Concept
Definition
Poly: many
Morphism: forms
Polymorphism is the
ability of an object to
take on many forms
What makes an object polymorphic?
An object is polymorphic if it satisfies more
than one IS-A relationship
Technically, all objects are polymorphic
because they will pass IS-A for their own type
and the Object class
IS-A Example
Consider a rose:
public class Plant {}
public class Flower extends Plant {}
public class Rose extends Flower {}
A rose IS A rose.
A rose IS A flower.
A rose IS A plant.
A rose IS A(n) object.
It’s Common
The most common type of polymorphism is
when a parent class reference type is used to
refer to a child object.
Polymorphism Example
Because of polymorphism, we can create rose
objects that have various reference types,
because the rose object is a child of the other
classes.
Rose rose = new Rose();
Flower flower = new Rose();
Plant plant = new Rose();
Object object = new Rose();
So what..?
So what is the use of creating an object with a
different reference variable type?
The reference variable type determines the
methods that can be called on the object.
Plant Class
So let’s say we define the classes as:
public class Plant {
public void photosynthesize() {
System.out.println(“Converted light energy to chemical energy.”);
}
}
Flower Class
And the flower class as:
public class Flower extends Plant {
public void bloom() {
System.out.println(“Flowers bloomed.”);
}
}
Rose Class
And the rose class as:
public class Rose extends Flower {
public void bloom() {
System.out.println(“Roses bloomed.”);
}
public void growThorns() {
System.out.println(“Thorns grew.”);
}
}
What does this mean?
When we create a rose object with the rose reference
variable, we can access all the methods defined in the rose
class.
Rose rose = new Rose();
rose.growThorns();
rose.bloom();
Thorns grew.
Roses bloomed.
Reference Type
When we use a different reference type, only the methods
defined in the reference type are used but they are
implemented according to how the object defines them, if it
overrides the parent’s method.
Flower flower = new Rose();
flower.bloom();
Roses bloomed.
Reference Type
If we try to use a method that’s not defined in the reference
type, the code will not work.
Flower flower = new Rose();
flower.bloom();
flower.growThorns();
Roses bloomed.
Error
Reference Type
So if we use the Plant reference type, because the only
defined method is photosynthesize, the only method
(besides the inherent Object methods) that is usable is
photosynthesize.
Plant plant = new Rose();
plant.photosynthesize();
plant.growThorns();
plant.bloom();
What do we call this?
We call the changing of reference types in
relation to objects upcasting and downcasting.
Upcasting is casting to a supertype, while
downcasting is casting to a subtype.
Is it safe?
Upcasting is always allowed and safe, and is
used pretty commonly.
Downcasting can cause errors, and requires a
type check.
Upcasting
Let’s revisit the rose example. We utilized
upcasting when we assigned the parent
reference type to the child object:
Flower flower = new Rose();
Plant plant = new Rose();
Object object = new Rose();
Downcasting
Downcasting is casting to a subtype, so basically giving a
child reference name to a parent object. This isn’t used very
often, and can cause the ClassCastException. This will
usually give a runtime error.
Plant plant = new Plant();
Rose rose = (Rose)plant;
Downcasting
The only real use of downcasting is to compare
one object to another, but otherwise, it has no
significant applications.
Runtime Polymorphism

More Related Content

Recently uploaded

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 

Recently uploaded (20)

What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Runtime Polymorphism

  • 2. Definition Poly: many Morphism: forms Polymorphism is the ability of an object to take on many forms
  • 3. What makes an object polymorphic? An object is polymorphic if it satisfies more than one IS-A relationship Technically, all objects are polymorphic because they will pass IS-A for their own type and the Object class
  • 4. IS-A Example Consider a rose: public class Plant {} public class Flower extends Plant {} public class Rose extends Flower {} A rose IS A rose. A rose IS A flower. A rose IS A plant. A rose IS A(n) object.
  • 5. It’s Common The most common type of polymorphism is when a parent class reference type is used to refer to a child object.
  • 6. Polymorphism Example Because of polymorphism, we can create rose objects that have various reference types, because the rose object is a child of the other classes. Rose rose = new Rose(); Flower flower = new Rose(); Plant plant = new Rose(); Object object = new Rose();
  • 7. So what..? So what is the use of creating an object with a different reference variable type? The reference variable type determines the methods that can be called on the object.
  • 8. Plant Class So let’s say we define the classes as: public class Plant { public void photosynthesize() { System.out.println(“Converted light energy to chemical energy.”); } }
  • 9. Flower Class And the flower class as: public class Flower extends Plant { public void bloom() { System.out.println(“Flowers bloomed.”); } }
  • 10. Rose Class And the rose class as: public class Rose extends Flower { public void bloom() { System.out.println(“Roses bloomed.”); } public void growThorns() { System.out.println(“Thorns grew.”); } }
  • 11. What does this mean? When we create a rose object with the rose reference variable, we can access all the methods defined in the rose class. Rose rose = new Rose(); rose.growThorns(); rose.bloom(); Thorns grew. Roses bloomed.
  • 12. Reference Type When we use a different reference type, only the methods defined in the reference type are used but they are implemented according to how the object defines them, if it overrides the parent’s method. Flower flower = new Rose(); flower.bloom(); Roses bloomed.
  • 13. Reference Type If we try to use a method that’s not defined in the reference type, the code will not work. Flower flower = new Rose(); flower.bloom(); flower.growThorns(); Roses bloomed. Error
  • 14. Reference Type So if we use the Plant reference type, because the only defined method is photosynthesize, the only method (besides the inherent Object methods) that is usable is photosynthesize. Plant plant = new Rose(); plant.photosynthesize(); plant.growThorns(); plant.bloom();
  • 15. What do we call this? We call the changing of reference types in relation to objects upcasting and downcasting. Upcasting is casting to a supertype, while downcasting is casting to a subtype.
  • 16. Is it safe? Upcasting is always allowed and safe, and is used pretty commonly. Downcasting can cause errors, and requires a type check.
  • 17. Upcasting Let’s revisit the rose example. We utilized upcasting when we assigned the parent reference type to the child object: Flower flower = new Rose(); Plant plant = new Rose(); Object object = new Rose();
  • 18. Downcasting Downcasting is casting to a subtype, so basically giving a child reference name to a parent object. This isn’t used very often, and can cause the ClassCastException. This will usually give a runtime error. Plant plant = new Plant(); Rose rose = (Rose)plant;
  • 19. Downcasting The only real use of downcasting is to compare one object to another, but otherwise, it has no significant applications.