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!
By the end of this module, you will be able to:
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."
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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!
| Word | Simple Definition |
|---|---|
| Inheritance | Inheriting from another class. |
| extends | Keyword to inherit. |
| super | Call the parent class. |
| Override | Replace a method. |
| Polymorphism | Many forms. |
| Abstract | Cannot be instantiated. |
| Protected | Visible in subclasses. |
| Public | Visible everywhere. |
| Private | Visible only in the class. |
| Class | A blueprint for objects. |
class Animal {
name: string;
}
class Dog extends Animal {
breed: string;
}
let animal: Animal = new Dog(); animal.makeSound(); // Bark
+-----------------------+-----------------------+ | 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 | +-----------------------+-----------------------+
| Modifier | Accessible In |
|---|---|
| public | Everywhere |
| protected | Subclasses |
| private | Only the class |
| Type | Description | Example |
|---|---|---|
| Single | One parent | Dog extends Animal |
| Multi-level | Chain of inheritance | Dog extends Animal; Animal extends LivingBeing |
| Hierarchical | Multiple children | Dog, Cat extends Animal |
(Included after each lesson above β review them!)
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!
Match the term with its definition.
| Term | Definition |
|---|---|
| 1. Inheritance | A. Inheriting from another class |
| 2. extends | B. Keyword to inherit |
| 3. super | C. Call parent class |
| 4. Override | D. Replace a method |
| 5. Polymorphism | E. Many forms |
Answers: 1-A, 2-B, 3-C, 4-D, 5-E
β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.
β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.
β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.
β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.
βInheritance Challengeβ
Create a complex class hierarchy using inheritance and polymorphism. Include abstract classes and method overriding. Share your solution with the class.
(Answers to fill-in-the-blank, true/false, and multiple choice are provided within those sections.)
In Module Seven, you will learn about generics. To prepare:
Get ready to learn about generics!
End of Module Six Β· You are now an inheritance master!