Card 09/ 09
Recap: Polymorphism
What the object decides, what the declared type decides, hiding, casts and chains, with where each was.
Polymorphism comes down to the five ideas below, each listed with the card that covers it.
| The idea | Where |
|---|---|
| An overridden method is chosen by the object's class, not the variable's type | Polymorphism: The Object Chooses Which Method Body Runs |
| The declared type decides what may be called; the object decides what runs | Declared Type vs Runtime Type |
| Fields and static methods are hidden, not overridden, and resolve from the declared type | Field Hiding: Why Fields Do Not Override Like Methods |
| A cast is a promise the compiler accepts and the runtime checks | ClassCastException: A Cast That Compiled and Then Threw |
| Put the method on the type when types change; use a chain when operations do | Polymorphic Methods vs instanceof Chains |
Exactly one thing in Java is chosen by the object while the program runs, and that is an overridden instance method. Fields, static methods and overloads are all chosen earlier, from what the source says the type is.
← → move through sections and cards