Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Certified Typescript Programming Expert Β· Lesson 7 of 16

Module Six

πŸ“– 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.
7
Module 6 Β· Certified TypeScript Programming Expert Β· Beginners

πŸ“˜ Module Six: Inheritance and Polymorphism – Building on What Exists


πŸ“– Module Introduction

Hello, inheritor! In Module Five, you learned about classes and objects. Now it is time to learn about inheritance and polymorphism – two powerful concepts that help you build on existing code.

Inheritance allows a class to inherit properties and methods from another class. Polymorphism allows objects to take many forms. These concepts make your code more organised, reusable, and flexible.

In this module, you will learn how to use inheritance to extend classes, override methods, and use polymorphism to write more flexible code.

Let's start building on what exists with TypeScript!


🎯 Learning Objectives

By the end of this module, you will be able to:

  • Understand what inheritance is.
  • Use the extends keyword to inherit a class.
  • Override methods in a subclass.
  • Understand polymorphism.
  • Use polymorphism in your code.

πŸ“š Warm-up Story: The Animal Kingdom

In a Nigerian village, a teacher named Mrs. Okafor was explaining animals to her students. She said, "All animals have a name and an age. Some animals walk, some swim, and some fly."

She created a class called Animal with name and age. Then she created subclasses: Dog, Fish, and Bird. Each subclass inherited from Animal and added its own special behaviour.

The students understood that all animals share common things, but each is special. Mrs. Okafor said, "This is like inheritance in programming – you build on what exists."


πŸ“˜ Main Lessons

Lesson 1: What is Inheritance?

Definition: Inheritance is a way for a class to inherit properties and methods from another class.

Why is it important? It promotes reusability and reduces duplication.

Simple explanation: It's like a child inheriting traits from a parent.

Real-life example: A child inherits their parent's eyes.

School example: A student inherits traits from their family.

Home example: A child inherits their parent's habits.

Nigerian example: A child inherits their parent's cultural traditions.

   class Animal {
     name: string;
     age: number;
   }
   class Dog extends Animal {
     // Dog inherits name and age
   }

Mini summary: Inheritance is when a class inherits from another class.

Lesson 2: The extends Keyword

Definition: The extends keyword is used to inherit a class.

Why is it important? It tells TypeScript that a class is a child of another class.

Simple explanation: It's like saying "this class is a type of that class."

Real-life example: class Dog extends Animal.

School example: class Student extends Person.

Home example: class Child extends Parent.

Nigerian example: class Nigerian extends Person.

   class Animal {
     name: string;
   }
   class Dog extends Animal {
     // Dog has all Animal properties
   }

Mini summary: The extends keyword is used to inherit.

Lesson 3: Parent and Child Classes

Definition: The parent class is the class being inherited from. The child class is the class that inherits.

Why is it important? It defines the relationship between classes.

Simple explanation: Parent = base; child = derived.

Real-life example: Animal is parent; Dog is child.

School example: Person is parent; Student is child.

Home example: Parent is parent; Child is child.

Nigerian example: Person is parent; Nigerian is child.

   class Animal {
     // Parent class
   }
   class Dog extends Animal {
     // Child class
   }

Mini summary: Parent classes are inherited from; child classes inherit.

Lesson 4: super Keyword

Definition: The super keyword is used to call the parent class's constructor or methods.

Why is it important? It allows you to reuse parent logic.

Simple explanation: It's like calling your parent for help.

Real-life example: super(name) calls the parent constructor.

School example: super(name) calls the parent constructor.

Home example: super(name) calls the parent constructor.

Nigerian example: super(name) calls the parent constructor.

   class Animal {
     constructor(name: string) {
       this.name = name;
     }
   }
   class Dog extends Animal {
     constructor(name: string) {
       super(name); // call parent constructor
     }
   }

Mini summary: super calls the parent class.

Lesson 5: Overriding Methods

Definition: Overriding means providing a new implementation of a method in a child class.

Why is it important? It allows child classes to customise behaviour.

Simple explanation: You replace a parent method with your own.

Real-life example: A dog's makeSound() barks; a cat's makeSound() meows.

School example: A student's study() method is different from a teacher's.

Home example: A child's play() method is different from a parent's.

Nigerian example: A Nigerian's greet() method is different from an American's.

   class Animal {
     makeSound(): void {
       console.log("Some sound");
     }
   }
   class Dog extends Animal {
     makeSound(): void {
       console.log("Bark");
     }
   }

Mini summary: Overriding replaces a parent method.

Lesson 6: What is Polymorphism?

Definition: Polymorphism means "many forms" – it allows objects to be treated as their parent type.

Why is it important? It makes code more flexible.

Simple explanation: The same method can work differently for different classes.

Real-life example: An animal can be a dog, a cat, or a bird.

School example: A person can be a student or a teacher.

Home example: A family member can be a parent or a child.

Nigerian example: A citizen can be a Nigerian or a foreigner.

   let animal: Animal = new Dog();
   animal.makeSound(); // Bark

Mini summary: Polymorphism means many forms.

Lesson 7: Polymorphism in Action

Definition: Polymorphism allows you to use a child class where a parent class is expected.

Why is it important? It gives you flexibility.

Simple explanation: You can treat a dog as an animal.

Real-life example: function feed(animal: Animal) { animal.eat(); }

School example: function grade(person: Person) { person.study(); }

Home example: function care(family: Family) { family.love(); }

Nigerian example: function greet(person: Person) { person.sayHello(); }

   function feed(animal: Animal) {
     animal.eat();
   }
   let dog = new Dog();
   feed(dog); // Works because Dog is an Animal

Mini summary: Polymorphism allows flexibility.

Lesson 8: Method Overriding and Polymorphism

Definition: Method overriding is a key part of polymorphism.

Why is it important? It allows different classes to have different behaviours.

Simple explanation: Each class can implement the same method differently.

Real-life example: Dog barks; Cat meows.

School example: Student studies; Teacher teaches.

Home example: Child plays; Parent works.

Nigerian example: Nigerian greets; American greets.

   class Animal {
     makeSound(): void {
       console.log("Some sound");
     }
   }
   class Dog extends Animal {
     makeSound(): void {
       console.log("Bark");
     }
   }
   class Cat extends Animal {
     makeSound(): void {
       console.log("Meow");
     }
   }

Mini summary: Overriding enables polymorphism.

Lesson 9: Abstract Classes

Definition: Abstract classes cannot be instantiated – they are only used for inheritance.

Why is it important? They provide a blueprint for subclasses.

Simple explanation: They are like templates.

Real-life example: Animal is abstract; you can't create an Animal object.

School example: Person is abstract; you can't create a Person object.

Home example: Family is abstract; you can't create a Family object.

Nigerian example: Citizen is abstract; you can't create a Citizen object.

   abstract class Animal {
     abstract makeSound(): void;
   }
   class Dog extends Animal {
     makeSound(): void {
       console.log("Bark");
     }
   }

Mini summary: Abstract classes are templates.

Lesson 10: Abstract Methods

Definition: Abstract methods are methods that must be implemented by subclasses.

Why is it important? They force subclasses to provide behaviour.

Simple explanation: You say "you must do this."

Real-life example: Animal says "you must make a sound."

School example: Person says "you must study."

Home example: Family says "you must contribute."

Nigerian example: Citizen says "you must obey the law."

   abstract class Animal {
     abstract makeSound(): void;
   }
   class Dog extends Animal {
     makeSound(): void {
       console.log("Bark");
     }
   }

Mini summary: Abstract methods must be implemented.

Lesson 11: Access Modifiers and Inheritance

Definition: Access modifiers (public, private, protected) control what is accessible in subclasses.

Why is it important? They protect your code.

Simple explanation: Public is visible everywhere; protected is visible in subclasses; private is only visible in the class itself.

Real-life example: public: everyone sees it; protected: family sees it; private: only you see it.

School example: public: whole school; protected: class; private: only you.

Home example: public: all family; protected: parents; private: only you.

Nigerian example: public: all Nigerians; protected: your family; private: only you.

   class Animal {
     public name: string;
     protected age: number;
     private id: number;
   }
   class Dog extends Animal {
     // Can access name and age, but not id
   }

Mini summary: Access modifiers control visibility.

Lesson 12: Common Mistakes with Inheritance

Definition: Common mistakes are errors that beginners often make with inheritance.

Why is it important? Avoiding them saves time and frustration.

Simple explanation: Don't forget to call super().

Real-life example: Forgetting to call the parent constructor.

School example: Forgetting to call super in a subclass.

Home example: Forgetting to call super in a child class.

Nigerian example: Forgetting to call super in a derived class.

   class Dog extends Animal {
     constructor(name: string) {
       super(name); // Forgetting this causes an error
     }
   }

Mini summary: Always call super in subclasses.

Lesson 13: Best Practices for Inheritance

Definition: Best practices are the recommended ways to use inheritance.

Why is it important? They make your code clean and safe.

Simple explanation: Use inheritance when classes share common properties.

Real-life example: Use inheritance for Animal and Dog.

School example: Use inheritance for Person and Student.

Home example: Use inheritance for Family and Child.

Nigerian example: Use inheritance for Citizen and Nigerian.

   class Animal {
     name: string;
   }
   class Dog extends Animal {
     breed: string;
   }

Mini summary: Use inheritance when classes share commonality.

Lesson 14: Inheritance in Everyday Life

Definition: Inheritance is not just for code – it is everywhere in life.

Why is it important? It shows that inheritance is natural and easy to understand.

Simple explanation: You inherit traits from your parents.

Real-life example: A child inherits their parent's eyes.

School example: A student inherits traits from their family.

Home example: A child inherits habits from parents.

Nigerian example: A child inherits cultural traditions.

   Inheritance is like a child inheriting traits from a parent.

Mini summary: Inheritance is natural and everywhere.

Lesson 15: You – The Inheritance Master

Definition: You are now an inheritance master – you understand inheritance and polymorphism.

Why is it important? You can now write flexible, organised code.

Simple explanation: You have mastered inheritance.

Real-life example: You can now build class hierarchies.

School example: You can use inheritance in school projects.

Home example: You can practice at home.

Nigerian example: You can build apps with inheritance in Nigeria.

   Inheritance Master Checklist:
   - Uses extends to inherit
   - Uses super to call parent
   - Overrides methods
   - Understands polymorphism
   - Uses abstract classes

Mini summary: You are now an inheritance master!


πŸ“– Key Vocabulary

WordSimple Definition
InheritanceInheriting from another class.
extendsKeyword to inherit.
superCall the parent class.
OverrideReplace a method.
PolymorphismMany forms.
AbstractCannot be instantiated.
ProtectedVisible in subclasses.
PublicVisible everywhere.
PrivateVisible only in the class.
ClassA blueprint for objects.

🧩 Important Concepts

  • Inheritance allows a class to inherit from another class.
  • The extends keyword is used for inheritance.
  • super calls the parent class.
  • Polymorphism means many forms.
  • You are now an inheritance master.

πŸ”’ Step-by-Step Explanations

How to Use Inheritance (in 4 steps)

  1. Step 1: Define a parent class.
  2. Step 2: Use the extends keyword to create a child class.
  3. Step 3: Call super() in the child constructor.
  4. Step 4: Override methods if needed.

🌍 Real-life Examples

  • Animal: Dog, Cat, Bird inherit from Animal.
  • Vehicle: Car, Bike, Truck inherit from Vehicle.
  • Employee: Manager, Developer inherit from Employee.
  • Shape: Circle, Square inherit from Shape.
  • Person: Student, Teacher inherit from Person.

πŸ‡³πŸ‡¬ Nigerian Examples

  • Person: Nigerian, Ghanaian inherit from Person.
  • Animal: Goat, Cow inherit from Animal.
  • Vehicle: Car, Bike inherit from Vehicle.
  • Employee: Manager, Staff inherit from Employee.
  • Product: Food, Clothing inherit from Product.

🎈 Fun Examples Children Can Relate To

  • Pet: Dog, Cat inherit from Pet.
  • Game: ActionGame, PuzzleGame inherit from Game.
  • Toy: Car, Doll inherit from Toy.
  • Book: StoryBook, TextBook inherit from Book.
  • Friend: BestFriend, SchoolFriend inherit from Friend.

🏠 Everyday Examples

  • Family: Parent, Child inherit from Family.
  • Food: Rice, Beans inherit from Food.
  • Clothing: Shirt, Pants inherit from Clothing.
  • Transport: Car, Bike inherit from Transport.
  • Furniture: Chair, Table inherit from Furniture.

πŸ‘©β€πŸ« Teacher Notes

  • This module is about inheritance – keep it simple and practical.
  • Use many examples to illustrate each concept.
  • Encourage students to create their own class hierarchies.
  • Emphasise the importance of polymorphism.
  • Make it interactive with live coding.

πŸ‘ͺ Parent Tips

  • Encourage your child to think about inheritance in everyday life.
  • Help them understand the concept of parent and child classes.
  • Support their coding practice.
  • Discuss how inheritance helps organise code.
  • Celebrate their progress.

πŸ’‘ Interesting Facts

  • Inheritance is a key concept in object-oriented programming.
  • Polymorphism allows for flexible code.
  • Abstract classes are used as blueprints.
  • Access modifiers control visibility.
  • Inheritance promotes code reuse.

πŸ€” Did You Know?

  • Did you know that TypeScript supports single inheritance?
  • Did you know that polymorphism is a core concept in OOP?
  • Did you know that abstract classes cannot be instantiated?
  • Did you know that method overriding is also called method overwriting?

🧠 Remember This

  • Inheritance is building on what exists.
  • extends is used to inherit.
  • super calls the parent.
  • Polymorphism means many forms.
  • You are an inheritance master.

⚠️ Common Mistakes

  • Mistake 1: Forgetting to call super().
  • Mistake 2: Using private properties in subclasses.
  • Mistake 3: Overriding without using override keyword (optional).
  • Mistake 4: Not using polymorphism when needed.
  • Mistake 5: Using inheritance when composition would be better.

βœ… Best Practices

  • Always call super() in subclasses.
  • Always use protected for members that subclasses need.
  • Always use override when overriding methods.
  • Always use polymorphism for flexibility.
  • Always choose inheritance when classes share commonality.

🎨 ASCII Illustrations

Inheritance – Example

   class Animal {
     name: string;
   }
   class Dog extends Animal {
     breed: string;
   }

Polymorphism – Example

   let animal: Animal = new Dog();
   animal.makeSound(); // Bark

Comparison: Inheritance vs Composition

   +-----------------------+-----------------------+
   |  Inheritance          |  Composition          |
   +-----------------------+-----------------------+
   | "Is a" relationship   | "Has a" relationship  |
   | Child inherits parent | Object contains object|
   | Tight coupling        | Loose coupling        |
   | Reuse code            | More flexible         |
   | Simple                | More complex          |
   +-----------------------+-----------------------+

πŸ“Š Comparison Tables

Access Modifiers

ModifierAccessible In
publicEverywhere
protectedSubclasses
privateOnly the class

Inheritance Types

TypeDescriptionExample
SingleOne parentDog extends Animal
Multi-levelChain of inheritanceDog extends Animal; Animal extends LivingBeing
HierarchicalMultiple childrenDog, Cat extends Animal

πŸ“ Summary After Every Lesson

(Included after each lesson above – review them!)


πŸ“Œ End-of-Module Summary

In this module, you learned about inheritance and polymorphism. You discovered how to use the extends keyword, call super(), and override methods. You also learned about polymorphism, abstract classes, and access modifiers. You are now an inheritance master. In the next module, you will learn about generics. Congratulations on completing Module Six!


❓ Frequently Asked Questions

  1. What is inheritance? Inheriting from another class.
  2. What is the extends keyword? Used to inherit.
  3. What is super? Calls the parent class.
  4. What is method overriding? Replacing a method.
  5. What is polymorphism? Many forms.
  6. What is an abstract class? Cannot be instantiated.
  7. What is a protected member? Visible in subclasses.
  8. What is a public member? Visible everywhere.
  9. What is a private member? Visible only in the class.
  10. What is the benefit of inheritance? Code reuse.

πŸ“ Review Questions (15)

  1. What is inheritance?
  2. What keyword is used for inheritance?
  3. What is super used for?
  4. What is method overriding?
  5. What is polymorphism?
  6. What is an abstract class?
  7. What is a protected member?
  8. What is a public member?
  9. What is a private member?
  10. What is the benefit of inheritance?
  11. What is a common mistake?
  12. What is a best practice?
  13. How do you use inheritance?
  14. Give an example of inheritance.
  15. What makes you an inheritance master?

✏️ Fill-in-the-Blank Exercises

  1. __________ is inheriting from another class.
  2. The __________ keyword is used to inherit.
  3. __________ calls the parent class.
  4. __________ is replacing a method.
  5. __________ means many forms.
  6. An __________ class cannot be instantiated.
  7. A __________ member is visible in subclasses.
  8. A __________ member is visible everywhere.
  9. A __________ member is visible only in the class.
  10. You are an __________ master.

βœ… True or False Exercises

  1. Inheritance is inheriting from another class. (True)
  2. extends is used for inheritance. (True)
  3. super calls the child class. (False)
  4. Method overriding replaces a method. (True)
  5. Polymorphism means many forms. (True)
  6. An abstract class can be instantiated. (False)
  7. A protected member is visible in subclasses. (True)
  8. A public member is visible everywhere. (True)
  9. A private member is visible in subclasses. (False)
  10. You are an inheritance master. (True)

πŸ”˜ Multiple Choice Questions (15 with answers)

  1. What is inheritance?
    a) Inheriting from another class
    b) A game
    c) A video
    Answer: a
  2. What keyword is used for inheritance?
    a) extends
    b) implements
    c) uses
    Answer: a
  3. What is super used for?
    a) Call parent class
    b) Call child class
    c) A game
    Answer: a
  4. What is method overriding?
    a) Replacing a method
    b) Deleting a method
    c) A game
    Answer: a
  5. What is polymorphism?
    a) Many forms
    b) One form
    c) A game
    Answer: a
  6. What is an abstract class?
    a) Cannot be instantiated
    b) Can be instantiated
    c) A game
    Answer: a
  7. What is a protected member?
    a) Visible in subclasses
    b) Visible everywhere
    c) A game
    Answer: a
  8. What is a public member?
    a) Visible everywhere
    b) Visible in subclasses
    c) A game
    Answer: a
  9. What is a private member?
    a) Visible only in the class
    b) Visible in subclasses
    c) A game
    Answer: a
  10. What is the benefit of inheritance?
    a) Code reuse
    b) Code duplication
    c) A game
    Answer: a
  11. What is a common mistake?
    a) Forgetting to call super
    b) Calling super
    c) Using inheritance
    Answer: a
  12. What is a best practice?
    a) Call super in subclasses
    b) Ignore super
    c) Use private everywhere
    Answer: a
  13. What is the purpose of inheritance?
    a) Reuse code
    b) Slow down code
    c) Confuse developers
    Answer: a
  14. Can you inherit from multiple classes?
    a) No
    b) Yes
    Answer: a
  15. Are you an inheritance master?
    a) Yes
    b) No
    Answer: a

πŸ”— Matching Exercises

Match the term with its definition.

TermDefinition
1. InheritanceA. Inheriting from another class
2. extendsB. Keyword to inherit
3. superC. Call parent class
4. OverrideD. Replace a method
5. PolymorphismE. Many forms

Answers: 1-A, 2-B, 3-C, 4-D, 5-E


πŸ“ Short Answer Questions

  1. What is inheritance?
  2. What is the difference between extends and implements?
  3. What is method overriding?
  4. What is polymorphism?
  5. What is an abstract class?

🎭 Scenario-based Exercises

  1. Scenario 1: You need to create a class hierarchy for animals. What would you use?
  2. Scenario 2: You need to allow different animals to make different sounds. What would you use?
  3. Scenario 3: You need to create a blueprint for classes that cannot be instantiated. What would you use?

πŸ‘₯ Group Activity

β€œInheritance Design”

In groups, design a class hierarchy for a real-world scenario (like a store or a school). Use inheritance and polymorphism. Share your design with the class.


πŸ§‘β€πŸ’» Individual Activity

β€œMy Inheritance Code”

Write TypeScript code using inheritance and polymorphism. Include a parent class, child classes, and method overriding. Share your code with the class.


πŸ—£οΈ Classroom Discussion Questions

  1. Why is inheritance useful?
  2. What is the difference between inheritance and composition?
  3. When would you use an abstract class?
  4. How does polymorphism make code flexible?
  5. What would you like to learn next?

πŸ› οΈ Mini Project

β€œInheritance Project”

Create a class hierarchy for a business (like a store or a school). Use inheritance, method overriding, and polymorphism. Present your project to the class.


πŸ’» Practical Assignment

β€œWrite Inheritance Code”

Write a TypeScript program that uses inheritance and polymorphism. Include a parent class, child classes, and method overriding. Compile and run it. Share your code and output.


⚑ Challenge Exercise

β€œInheritance Challenge”

Create a complex class hierarchy using inheritance and polymorphism. Include abstract classes and method overriding. Share your solution with the class.


πŸ“‹ Quiz Answers

(Answers to fill-in-the-blank, true/false, and multiple choice are provided within those sections.)


🎁 Key Takeaways

  • Inheritance is inheriting from another class.
  • extends is used for inheritance.
  • super calls the parent class.
  • Polymorphism means many forms.
  • You are an inheritance master.

πŸš€ Preparation for Module Seven

In Module Seven, you will learn about generics. To prepare:

  • Review what you learned about types.
  • Think about how you could make code more flexible with types.
  • Bring your questions to the next class.

Get ready to learn about generics!


End of Module Six Β· You are now an inheritance master!

πŸ† 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.
β†’