Hello, object builder! In Module Four, you learned about functions. Now it is time to learn about classes and objects β the blueprints for creating real-world things in code.
Classes are like blueprints or templates. They describe how to create objects. Objects are things you create from classes β like a specific person, car, or product.
In this module, you will learn how to create classes, use constructors, add properties and methods, and create objects from your classes.
Let's start building with classes and objects!
By the end of this module, you will be able to:
In Lagos, a car company wanted to build many cars. Instead of building each car from scratch, they created a blueprint β a design that described what a car should look like and how it should work.
The blueprint said: "A car has a brand, a model, and a colour. It can start, stop, and drive."
Using this blueprint, they could create many cars β each with different brands, models, and colours.
In programming, a class is like that blueprint. An object is the actual car built from the blueprint. This is what we will learn in this module!
Definition: A class is a blueprint for creating objects.
Why is it important? Classes help you organise and reuse code.
Simple explanation: A class is like a recipe β it tells you how to make something.
Real-life example: A blueprint for a house.
School example: A template for a student record.
Home example: A recipe for a meal.
Nigerian example: A design for a traditional outfit.
class Car {
// Blueprint for cars
}
Mini summary: A class is a blueprint for objects.
Definition: An object is an instance of a class β a real thing created from the blueprint.
Why is it important? Objects are what you actually use in your program.
Simple explanation: An object is a specific thing made from a class.
Real-life example: A specific car (Toyota Camry) is an object.
School example: A specific student is an object.
Home example: A specific meal is an object.
Nigerian example: A specific Ankara outfit is an object.
let myCar = new Car();
Mini summary: An object is an instance of a class.
Definition: You create a class using the class keyword.
Why is it important? It defines the structure for your objects.
Simple explanation: You write a class to describe what objects will look like.
Real-life example: class Car { }
School example: class Student { }
Home example: class Recipe { }
Nigerian example: class Outfit { }
class Car {
brand: string;
model: string;
}
Mini summary: The class keyword creates a class.
Definition: Properties are the data that a class holds β like brand, model, or colour.
Why is it important? They define the characteristics of an object.
Simple explanation: Properties are the attributes of an object.
Real-life example: A car has brand, model, and colour.
School example: A student has name, grade, and class.
Home example: A recipe has name, ingredients, and instructions.
Nigerian example: An outfit has type, colour, and size.
class Car {
brand: string;
model: string;
colour: string;
}
Mini summary: Properties are the data of a class.
Definition: Methods are functions inside a class that define behaviour.
Why is it important? They describe what objects can do.
Simple explanation: Methods are actions that an object can perform.
Real-life example: A car can start, stop, and drive.
School example: A student can study, take tests, and graduate.
Home example: A recipe can be cooked and served.
Nigerian example: An outfit can be worn and washed.
class Car {
start(): void {
console.log("Car started");
}
}
Mini summary: Methods are actions a class can perform.
Definition: A constructor is a special method that initialises an object when it is created.
Why is it important? It sets up the initial state of an object.
Simple explanation: A constructor is like the first step in building something.
Real-life example: A car is assembled at the factory.
School example: A student is enrolled in a school.
Home example: A meal is prepared before cooking.
Nigerian example: An outfit is sewn by a tailor.
class Car {
brand: string;
constructor(brand: string) {
this.brand = brand;
}
}
Mini summary: Constructors initialise objects.
Definition: this refers to the current object.
Why is it important? It helps you access properties and methods inside the class.
Simple explanation: this is like saying "me" or "myself."
Real-life example: this.brand refers to the brand of this car.
School example: this.name refers to the name of this student.
Home example: this.ingredients refers to the ingredients of this recipe.
Nigerian example: this.type refers to the type of this outfit.
class Car {
brand: string;
constructor(brand: string) {
this.brand = brand;
}
}
Mini summary: this refers to the current object.
Definition: You create an object using the new keyword.
Why is it important? It instantiates a class into an object.
Simple explanation: You use new to build an object from a class.
Real-life example: let myCar = new Car("Toyota");
School example: let student = new Student("Ada");
Home example: let recipe = new Recipe("Jollof Rice");
Nigerian example: let outfit = new Outfit("Ankara");
let myCar = new Car("Toyota");
console.log(myCar.brand); // Toyota
Mini summary: The new keyword creates objects.
Definition: Public means the property or method is accessible from anywhere.
Why is it important? It allows access to the member.
Simple explanation: Everyone can see it.
Real-life example: Public information.
School example: A public announcement.
Home example: A shared family calendar.
Nigerian example: Public services.
class Car {
public brand: string;
}
Mini summary: Public members are accessible anywhere.
Definition: Private means the property or method is accessible only inside the class.
Why is it important? It protects data from outside access.
Simple explanation: Only the class can see it.
Real-life example: A personal diary.
School example: A student's private notes.
Home example: A family secret.
Nigerian example: Personal information.
class Car {
private engine: string;
}
Mini summary: Private members are only accessible inside the class.
Definition: Protected means the property or method is accessible in the class and its subclasses.
Why is it important? It allows inheritance.
Simple explanation: The class and its children can see it.
Real-life example: Family heirlooms.
School example: A teacher's lesson notes shared with student teachers.
Home example: A parent's advice to their children.
Nigerian example: Cultural traditions passed down.
class Car {
protected engine: string;
}
Mini summary: Protected members are accessible in subclasses.
Definition: Getters and setters are methods that control access to properties.
Why is it important? They allow you to add logic when getting or setting values.
Simple explanation: They are like guards for your properties.
Real-life example: A security guard at a gate.
School example: A teacher checking homework.
Home example: A parent checking on a child.
Nigerian example: A security checkpoint.
class Car {
private _brand: string;
get brand(): string {
return this._brand;
}
set brand(value: string) {
this._brand = value;
}
}
Mini summary: Getters and setters control access to properties.
Definition: Static members belong to the class itself, not instances.
Why is it important? They are useful for utility functions.
Simple explanation: They are shared by all objects.
Real-life example: A factory that builds cars.
School example: A school's motto.
Home example: A family name.
Nigerian example: A national anthem.
class Car {
static brand: string = "Toyota";
}
console.log(Car.brand);
Mini summary: Static members belong to the class.
Definition: Common mistakes are errors that beginners often make with classes.
Why is it important? Avoiding them saves time and frustration.
Simple explanation: Don't forget to use the new keyword.
Real-life example: Forgetting to use new when creating an object.
School example: Forgetting to instantiate a student.
Home example: Forgetting to prepare ingredients before cooking.
Nigerian example: Forgetting to use new when creating an object.
// Error: missing new
let myCar = Car("Toyota");
Mini summary: Always use new to create objects.
Definition: You are now a class master β you understand classes and objects.
Why is it important? You can now build real-world applications.
Simple explanation: You have mastered classes.
Real-life example: You can now create classes for any purpose.
School example: You can use classes in school projects.
Home example: You can practice at home.
Nigerian example: You can build apps with classes in Nigeria.
Class Master Checklist: - Creates classes - Uses constructors - Adds properties and methods - Creates objects - Uses access modifiers
Mini summary: You are now a class master!
| Word | Simple Definition |
|---|---|
| Class | A blueprint for objects. |
| Object | An instance of a class. |
| Property | A piece of data in a class. |
| Method | A function in a class. |
| Constructor | Initialises an object. |
| this | Refers to the current object. |
| Public | Accessible anywhere. |
| Private | Accessible only in the class. |
| Protected | Accessible in subclasses. |
| Static | Belongs to the class. |
class Car {
brand: string;
model: string;
constructor(brand: string, model: string) {
this.brand = brand;
this.model = model;
}
start(): void {
console.log("Car started");
}
}
let myCar = new Car("Toyota", "Camry");
console.log(myCar.brand); // Toyota
myCar.start(); // Car started
+-----------------------+-----------------------+ | Class | Object | +-----------------------+-----------------------+ | Blueprint | Instance | | Template | Real thing | | Defines structure | Holds data | | Not usable directly | Usable | | One class | Many objects | +-----------------------+-----------------------+
| Modifier | Accessible In |
|---|---|
| public | Everywhere |
| private | Only the class |
| protected | Class and subclasses |
| Component | Description | Example |
|---|---|---|
| Property | Data | brand: string |
| Method | Action | start(): void |
| Constructor | Initialisation | constructor() |
| Static | Class-level | static brand: string |
(Included after each lesson above β review them!)
In this module, you learned about classes and objects. You discovered how to create classes, use constructors, add properties and methods, and create objects. You also learned about access modifiers (public, private, protected) and static members. You are now a class master. In the next module, you will learn about inheritance and polymorphism. Congratulations on completing Module Five!
Match the term with its definition.
| Term | Definition |
|---|---|
| 1. Class | A. Blueprint for objects |
| 2. Object | B. Instance of a class |
| 3. Property | C. Data in a class |
| 4. Method | D. Action in a class |
| 5. Constructor | E. Initialises an object |
Answers: 1-A, 2-B, 3-C, 4-D, 5-E
βClass Designβ
In groups, design a class for a real-world scenario (like a store or a school). Include properties, methods, and a constructor. Share your design with the class.
βMy Classβ
Write a TypeScript class for a real-world thing (like a car, person, or product). Include properties, a constructor, and methods. Create an object from your class. Share your code with the class.
βClass Projectβ
Create a class for a business (like a store or a school). Include properties, a constructor, and methods. Create objects from your class and use them. Present your project to the class.
βWrite TypeScript Classesβ
Write a TypeScript program with a class and objects. Include properties, a constructor, and methods. Compile and run it. Share your code and output.
βClass Challengeβ
Create a complex class with multiple properties, methods, and access modifiers. Include static members. Create objects and use them. Share your solution with the class.
(Answers to fill-in-the-blank, true/false, and multiple choice are provided within those sections.)
In Module Six, you will learn about inheritance and polymorphism. To prepare:
Get ready to learn about inheritance!
End of Module Five Β· You are now a class master!