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.