Polymorphism under the hood 00:00:00 Make a prediction You will explain how a virtual call selects an implementation and distinguish common compiler mechanisms from guarantees every language must provide. A function receives a shape reference and calls area. It later receives a different shape through the same variable. Does the compiled call site have to change? No. In a common compiled design, the receiver supplies dispatch information that leads to its area implementation. The same call site can reach circle area or rectangle area. 00:00:34 Number two! A coach calls out exercise two. A beginner wearing a white band checks the white instruction board; an advanced athlete checks the green board. The shouted number stays fixed but the action depends on the athlete's board. Think of the exercise number as a method slot and the receiver's band as the link to its own implementation table. This is a model of a common dispatch mechanism, not a required layout for every language. Polymorphism comes from Greek: poly is many, morph is form. One piece of code works with many kinds of object. Polymorphism shows how to design with it. this design shows how the machine finds the right method. The caller was compiled before the class existed. A remote control written last year must work with a heater written today. So the address of the method cannot be in the caller. It must be found through the object. 00:01:31 Follow a virtual call A function accepts a shape reference and asks for area. Its source cannot know whether the caller supplied a circle or rectangle. In a common compiled implementation, the object leads to type-specific dispatch information. The area slot identifies the implementation for this receiver. Call again with a rectangle and the same call site reaches rectangle area. An optimizing compiler may remove that lookup when the receiver type is provably fixed. Put a circle behind a shape reference, invoke area, then replace only the object with a rectangle. The second call reaches rectangle's implementation even though the source expression is identical. Looking only at the variable's declared type cannot identify the target in this example. 00:02:19 Compare with a direct call A helper declared final or otherwise statically resolved may be called directly. That says nothing about which call dominates run time without measurement. If an interviewer asks about virtual dispatch costs, describe the extra indirection and then ask whether data layout or actual computation costs more. Make the concrete receiver known in a hot loop and time a real workload. A compiler may use a direct call or inline it when that substitution preserves behaviour. If a new receiver becomes possible, the implementation must still honour the public dispatch semantics. An optimizer that proves the receiver type may remove the indirect lookup without changing program behaviour. Observing a direct machine call does not disprove polymorphism. Use a concrete nonvirtual call when there is no substitutable behaviour to express; profile before redesigning solely for dispatch cost. The Itanium C plus plus binary interface documents one common virtual-table layout. The Linux virtual file system uses tables of operations to dispatch requests to the relevant file-system implementation. These are implementation mechanisms behind a contract; two languages can offer similar dynamic calls without promising identical object memory layouts. 00:03:42 Try an interview follow-up Why should a base class destructor be virtual? If I delete a derived object through a base pointer and the destructor is not virtual, the behaviour is undefined, and usually the derived part is not cleaned up. Virtual means that the destructor is found through the table. A protected non-virtual destructor is the other safe choice, because nobody can delete through the base. A profiler blames a render loop that draws mixed shapes. Does that justify replacing every virtual call with a type switch? Measure a representative loop with the same objects and work. Memory layout, allocation or drawing itself may dominate. Preserve observable dispatch behaviour and compare the measured alternatives before paying the maintenance cost of an explicit type switch. 00:04:33 What to take with you A class owns a table of functions. An object owns a pointer to that table. A call knows a slot number. Read the pointer, read the slot, jump, and pass the object along. A subclass starts with a copy of the table and replaces what it overrides. Languages differ in the search and agree on the idea. C++ and Java use tables, Python walks the MRO, JavaScript walks prototypes. All fast ones remember answers. The cost is small, and the important loss is inlining, so look at it only in hot loops that you measured.