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, al...
IS-A Example
Consider a rose:
public class Plant {}
public class Flower extends Plant {}
public class Rose extends Flower ...
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 ro...
So what..?
So what is the use of creating an object with a
different reference variable type?
The reference variable type ...
Plant Class
So let’s say we define the classes as:
public class Plant {
public void photosynthesize() {
System.out.println...
Flower Class
And the flower class as:
public class Flower extends Plant {
public void bloom() {
System.out.println(“Flower...
Rose Class
And the rose class as:
public class Rose extends Flower {
public void bloom() {
System.out.println(“Roses bloom...
What does this mean?
When we create a rose object with the rose reference
variable, we can access all the methods defined ...
Reference Type
When we use a different reference type, only the methods
defined in the reference type are used but they ar...
Reference Type
If we try to use a method that’s not defined in the reference
type, the code will not work.
Flower flower =...
Reference Type
So if we use the Plant reference type, because the only
defined method is photosynthesize, the only method
...
What do we call this?
We call the changing of reference types in
relation to objects upcasting and downcasting.
Upcasting ...
Is it safe?
Upcasting is always allowed and safe, and is
used pretty commonly.
Downcasting can cause errors, and requires ...
Upcasting
Let’s revisit the rose example. We utilized
upcasting when we assigned the parent
reference type to the child ob...
Downcasting
Downcasting is casting to a subtype, so basically giving a
child reference name to a parent object. This isn’t...
Downcasting
The only real use of downcasting is to compare
one object to another, but otherwise, it has no
significant app...
Runtime Polymorphism
of 20

Runtime Polymorphism

Overview of Polymorphism and its role in Object Oriented Programming. These slides fully cover runtime polymorphism with examples in Java
Published on: Mar 4, 2016
Published in: Software      
Source: www.slideshare.net


Transcripts - Runtime Polymorphism

  • 1. Polymorphism Object Oriented Programming Concept
  • 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.

Related Documents