Card 09/ 09

RecapDifficulty: Advanced1 min

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.

What the topic established, and which card it was on
The ideaWhere
An overridden method is chosen by the object's class, not the variable's typePolymorphism: The Object Chooses Which Method Body Runs
The declared type decides what may be called; the object decides what runsDeclared Type vs Runtime Type
Fields and static methods are hidden, not overridden, and resolve from the declared typeField Hiding: Why Fields Do Not Override Like Methods
A cast is a promise the compiler accepts and the runtime checksClassCastException: A Cast That Compiled and Then Threw
Put the method on the type when types change; use a chain when operations doPolymorphic 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.