Polymorphism 00:00:00 Make a prediction You will trace one call through two receivers and distinguish the operation promised at the call site from the implementation that runs. Checkout calls quote on a shipping option. The variable has the same declared type both times. Must standard and express shipping return the same fee? No. The common contract promises an operation, not identical prices. Runtime dispatch chooses the implementation belonging to the actual receiver. 00:00:33 Sam's wind-up bench A wind-up bench once had a different instruction sheet for every toy. Give each toy one winding key instead. Turn the same key on a frog and a train: one hops and the other rolls. The caller repeats one operation and trusts the particular receiver to decide how. A new toy fits without adding a branch to the person turning the key. Polymorphism means many forms, from the Greek poly (many) and morphe (form). In a program it means that one name, one call or one piece of code works with values of different types. Without it, every caller must ask: what kind are you? The answer is a chain of if or a switch, and the same chain is needed in every place that treats the kinds differently. A new kind means finding all of them. 00:01:24 Calculate delivery without a switch A basket costing fifty dollars is sent with a standard shipping option. The basket calls quote on the option it received. A standard option returns a flat fee; an express option can compute a different fee from the same basket. The runtime chooses the implementation. Test checkout twice using those options. Adding pickup with a zero fee needs a new implementation but no new type test in checkout. Keep the basket at fifty dollars while swapping only the receiver of the quote call. The same caller reaches a different calculation. Remove one policy implementation from registration and make the failure visible at selection. The checkout need not know the list of concrete pricing classes. 00:02:08 Draw two objects through one contract A canvas visits a circle and a rectangle and asks each to draw. The circle uses a centre and radius, the rectangle uses corners. The same draw call has two behaviours because the receiver differs. If a shape cannot draw at all, it does not fulfil this interface. Walk the list from left to right. The circle reads its radius, the rectangle its corners, and each draws once. Add a triangle. The canvas loop can remain the same as long as the new shape honours the same draw contract. An explicit shape-type switch would need another branch. If the caller still asks whether the receiver is a frog or a train before turning the key, the variation has leaked back into the caller. A simple switch can be clearer for a fixed, closed set with one operation; polymorphism pays off as independent implementations or behaviours grow. Java Collections. List is an interface. Code that takes a List works with ArrayList, LinkedList and the unmodifiable lists of List.of. The last ones show a weak contract too: add throws UnsupportedOperationException. Linux Virtual File System. The kernel documentation describes the VFS as the layer that lets different file system implementations coexist. open and read are the same calls for all of them. 00:03:35 Try an interview follow-up What is the difference between overriding and overloading? An override supplies a different body for the same inherited operation. At runtime the receiver determines which body runs. An overload offers several signatures with one name. In a language such as Java the compiler chooses among them using the declared argument types. Changing only the return type does not create another Java overload. Ask a notification service to send the same reminder by email and by text. Let both implement the delivery operation and report its result. Add a third channel and trace what the caller changes. Then test a failed send. All three channels must honor the shared failure contract as well as the successful one. 00:04:27 What to take with you The caller knows the contract, the object knows the behaviour. Static type decides what you may call, dynamic type decides what runs. Overriding is chosen at run time by the receiver. Overloading is chosen before that by the declared types of the arguments. Most puzzles in interviews mix these two.