Type System
Upcasting, Downcasting, and Type Checks.
Type System
Analogy: concept
Type Casting is like Disguises:
- Upcasting: A Doctor puts on a generic "Human" mask. They are still a Doctor, but you only treat them as a Human. (Safe).
- Downcasting: You ask the "Human" to take off the mask. If they really ARE a Doctor, great! If they are actually a Firefighter, you have a problem. (Unsafe).
The Transformer
See how objects can be viewed as their Parent type (Upcasting) or cast back to their specific type (Downcasting).
Type Casting (Transformer)
Original Object
Type: Circle
Object created as a Circle.
Key Concepts
-
Upcasting: Treating a Child object as a Parent object.
- Always safe.
Shape s = new Circle();
-
Downcasting: Treating a Parent reference as a Child object.
- Risky! Requires a check.
Circle c = (Circle) s;
-
Type Checking: Using
instanceof(Java) ortypeofto verify the object before casting.
The Code
class Shape {}
class Circle extends Shape { void roll() {} }
class Square extends Shape { void slide() {} }
// Upcasting (Safe)
Shape s = new Circle();
// Downcasting (Risky)
if (s instanceof Circle) {
Circle c = (Circle) s; // Safe now!
c.roll();
} else {
System.out.println("Not a Circle!");
}
// Bad Downcast
Square sq = (Square) s; // Error! ClassCastException.
Up Next
Immutable Objects