Inheritance lets a child class reuse and extend a parent class. It's like a family tree: every child gets certain traits from the parent, and can add their own.
But deep inheritance trees get messy fast. A hierarchy that's five levels deep is hard to understand and change. Prefer shallow hierarchies β one or two levels β and use composition for extra features.
When you call a method on a parent reference, the program picks the child's override at runtime β not the parent's. That's dynamic dispatch.
Under the hood, languages use a virtual method table (vtable): a lookup table that maps each method name to the right implementation for each class. When you call animal.speak(), the vtable points to Dog.speak() or Cat.speak() depending on the actual object.
An abstract class is a blueprint that can't be created directly β subclasses must fill in the blanks. An interface is a promise: "I will provide these methods." A mixin adds reusable behaviour to multiple unrelated classes.
Rule of thumb: use an interface when classes share capabilities (like Flyable). Use an abstract class when classes share code (like Shape providing area() logic). Use a mixin to sprinkle in a feature (like Loggable).
Generics let you write code that works with many types while keeping type safety. Instead of writing IntBox, StringBox, and DogBox, you write Box<T> β one class for all types.
Example: a List<String> can only hold strings. Try to add a number, and the compiler shouts at you before the program runs. That's the safety net.
Sometimes you want a List<Cat> to be usable as a List<Animal>, or the opposite. Variance decides which direction is safe.
Bounds limit which types can be used: <T extends Animal> means T must be an Animal or a subclass. Wildcards (in Java) let you say "any subtype of X" β useful when you don't know the exact type.
C++ templates and Java generics solve the same problem β but differently. C++ templates generate a separate version of the code for each type. Java generics use type erasure: the compiler checks types but the runtime doesn't see them.
Python's generics are mostly hints β checked by tools like mypy, but not enforced at runtime. Each language makes tradeβoffs; the important thing is that the idea of "write once, use for any type" is the same.
Shallow tree: Create an Animal base class with a speak() method, and three subclasses: Dog, Cat, Bird. Override speak() in each. Loop through a list and hear the chorus β that's polymorphism!
Vtable detective: Use a parent reference to hold a child object. Call an overridden method and see which version runs. Then call a nonβoverridden method and observe the parent version. Explain the difference.
Interface vs abstract: Create an interface Flyable with fly(), and an abstract class Vehicle with concrete start(). Make Airplane implement Flyable and extend Vehicle. Confirm both features work together.
Mixin magic: Write a Loggable mixin that adds a log() method. Mix it into two unrelated classes (e.g., User and Order). Show that both can log without sharing a parent.
Generic box: Write a Box<T> class with put(T) and get(). Use it with integers, strings, and a custom class. Notice the compiler warning when you try to mix types.
Bounds test: Write a generic function sumAll<T extends Number>(List<T>) that adds up any list of numbers. Try it with integers and doubles β then try it with strings and see the compiler refuse.
By the end of this module, you'll be able to: design shallow, readable inheritance trees, explain how dynamic dispatch works, choose between abstract classes, interfaces, and mixins, and write safe, reusable generic code.