Chapter 9 Revision: Polymorphic Structures
Polymorphism enables algorithms and data structures to operate seamlessly across diverse data representations without duplicating structural code. Mastering generic type parameters, memory representation trade-offs, virtual method dispatch (vtables), and subtyping variance is vital for advanced systems software.
Use these flashcards for active recall. Try to answer the question out loud before clicking the card to flip it.
Flashcards 🎴
What are the three primary forms of Polymorphism in programming languages?
2. Subtype Polymorphism (Inclusion): Types derived from a supertype substitutable at runtime via inheritance and dynamic dispatch.
3. Ad-hoc Polymorphism (Overloading / Type Classes): Functions with identical names behaving differently across specific distinct types.
What is Monomorphization versus Type Erasure?
• Type Erasure (Java): Checks types at compile time and erases type parameters to
Object (single shared bytecode, but incurs boxing and pointer indirection overhead).What is a Virtual Method Table (vtable) and how does Dynamic Dispatch work?
vptr[offset]()) in O(1) time at runtime.What is Covariance and Contravariance in generic subtyping?
? extends T / Out): Preserves subtyping direction (if Cat <: Animal, then List• Contravariance (
? super T / In): Reverses subtyping direction (Consumer(PECS: Producer Extends, Consumer Super).
Why is an array of a subtype NOT safely covariant in mutable languages (Array Covariance Problem)?
String[] is a subtype of Object[]. If assigned to Object[] arr, inserting an Integer (arr[0] = 42) compiles cleanly but triggers a runtime ArrayStoreException. True type safety requires invariance for mutable collections.