Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Fundamentals of Object Oriented Programming Level Three Β· Lesson 6 of 9

Module Five

πŸ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
6
Module 5: Inheritance, Polymorphism & Generics for Young Coders
⭐ Module 5 · Level Three

Inheritance, Polymorphism
& Generics

🧬 Reuse smartly, adapt flexibly, and stay type-safe

🎯 What you'll learn in this module

  • βœ” How to build inheritance hierarchies without tangling them
  • βœ” How dynamic dispatch and virtual method tables make polymorphism work
  • βœ” When to use abstract classes, interfaces, and mixins
  • βœ” How generics let you write code that works with any type
  • βœ” What variance, bounds, and wildcards mean
  • βœ” How templates in C++ compare with generics in Java and Python
🌳
Inheritance: The Family Tree
Inheritance

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.

πŸš— Vehicle
↓
πŸš™ Car
🏍️ Motorcycle
🚚 Truck
πŸ‘¨β€πŸ‘©β€πŸ‘§ A shallow family tree is easy to read. A 20‑generation family tree? Not so much.
🎯
Dynamic Dispatch: The Right Method at the Right Time
Polymorphism

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.

πŸ“ž You call "the manager" β€” whoever holds the manager role right now answers, not the building's default phone.
🧩
Abstract Classes, Interfaces & Mixins
Reuse

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).

πŸŽ“ Interface = a diploma (a promise). Abstract class = a course syllabus (partly filled in). Mixin = a reusable skill you can add to any career.
πŸ“¦
Generics: Write Once, Work With Any Type
Generics

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.

🏷️ Like a labelled storage box: Box<Shoes> only accepts shoes. Fill it with socks, and the box refuses.
πŸ“
Variance, Bounds & Wildcards
Generics

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.

🏟️ A stadium ticket might say "any seat in Row A" (wildcard) or "seat 12" (exact type). Both are valid, but one is more flexible.
πŸ”§
Templates vs. Generics: Same Idea, Different Engines
Comparison

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.

🍳 One chef makes a fresh pancake per order (C++), another preps one big batter (Java). Both serve pancakes; the kitchen workflow differs.
⚠️ Classic trap: Deep inheritance trees are tempting but often lead to rigid, confusing code. If a class is struggling to fit its parent, that's your sign to switch to composition or an interface. Reuse is great β€” but only when it stays readable.

πŸ› οΈ Hands-on activities (no boring stuff!)

1

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!

2

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.

3

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.

4

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.

5

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.

6

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.

πŸ† Get Certified

πŸ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

πŸŽ“ Sign Up to Get Certified
πŸ› οΈ Practice Tools
Hands-on simulators & labs - subscription required.
β†’
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
β†’