0
Challenge · 80CS Fundamentals
Polymorphism: compile time vs runtime
OOPused in ~90% of apps
One call, many behaviours, chosen by the type of the thing being called on. It is what makes a mixed list of shapes summable in a single loop, and the interview asks specifically for the difference between the version decided while compiling and the version decided while running.
Core idea. Runtime polymorphism means the same call reaches different code depending on what the object actually is, decided while the program runs. Compile time polymorphism means the same name refers to different functions chosen by their arguments, decided before the program runs. Only the first is the interesting one.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
tick the behaviours an item needs
with inheritance
class PlainItem
One class per combination. Three behaviours need 8 classes, and a fourth needs 16.
with composition
traits: []
One item, a list of behaviours. Three behaviours need 3 functions, and a fourth needs 4.
Inheritance grows as two to the number of behaviours. Composition grows as the number of behaviours.
↑ play a little first, then dig in