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

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 Β· Certified TypeScript Programming Expert Β· Beginners

πŸ“˜ Module Five: Classes and Objects – Blueprints for Real-World Things


πŸ“– Module Introduction

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!


🎯 Learning Objectives

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

  • Understand what classes are.
  • Create classes with properties and methods.
  • Use constructors to initialise objects.
  • Create objects from classes.
  • Use access modifiers (public, private, protected).

πŸ“š Warm-up Story: The Blueprint for a Car

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!


πŸ“˜ Main Lessons

Lesson 1: What is a Class?

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.

Lesson 2: What is an Object?

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.

Lesson 3: Creating 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.

Lesson 4: Properties

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.

Lesson 5: Methods

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.

Lesson 6: Constructors

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.

Lesson 7: The this Keyword

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.

Lesson 8: Creating an 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.

Lesson 9: Access Modifiers – Public

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.

Lesson 10: Access Modifiers – Private

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.

Lesson 11: Access Modifiers – Protected

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.

Lesson 12: Getters and Setters

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.

Lesson 13: Static Members

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.

Lesson 14: Common Mistakes with Classes

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.

Lesson 15: You – The Class Master

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!


πŸ“– Key Vocabulary

WordSimple Definition
ClassA blueprint for objects.
ObjectAn instance of a class.
PropertyA piece of data in a class.
MethodA function in a class.
ConstructorInitialises an object.
thisRefers to the current object.
PublicAccessible anywhere.
PrivateAccessible only in the class.
ProtectedAccessible in subclasses.
StaticBelongs to the class.

🧩 Important Concepts

  • Classes are blueprints for objects.
  • Objects are instances of classes.
  • Properties are data; methods are actions.
  • Constructors initialise objects.
  • Access modifiers control visibility.

πŸ”’ Step-by-Step Explanations

How to Create a Class and Object (in 4 steps)

  1. Step 1: Define the class using the class keyword.
  2. Step 2: Add properties with types.
  3. Step 3: Add a constructor to initialise properties.
  4. Step 4: Create an object using the new keyword.

🌍 Real-life Examples

  • Car: brand, model, colour, start(), stop().
  • Person: name, age, email, greet().
  • Product: name, price, description, getPrice().
  • Student: name, grade, class, study().
  • Order: id, items, total, calculateTotal().

πŸ‡³πŸ‡¬ Nigerian Examples

  • Customer: name, age, email, city, greet().
  • Product: name, price, description, isAvailable, getPrice().
  • Student: name, grade, school, isGraduated, study().
  • Employee: name, position, salary, isActive, work().
  • Order: id, items, total, isDelivered, calculateTotal().

🎈 Fun Examples Children Can Relate To

  • Pet: name, age, isFriendly, play().
  • Game: name, level, isCompleted, start().
  • Friend: name, age, likesPizza, hangout().
  • Book: title, pages, isRead, read().
  • Movie: title, rating, isWatched, watch().

🏠 Everyday Examples

  • Grocery: item, quantity, isBought, buy().
  • Chore: task, priority, isDone, doTask().
  • Contact: name, phone, isFavorite, call().
  • Note: title, content, isImportant, edit().
  • Expense: amount, category, isPaid, pay().

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

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

πŸ‘ͺ Parent Tips

  • Encourage your child to think about classes and objects in everyday life.
  • Help them understand the difference between a class and an object.
  • Support their coding practice.
  • Discuss how classes help organise code.
  • Celebrate their progress.

πŸ’‘ Interesting Facts

  • Classes are a key part of object-oriented programming.
  • TypeScript classes are similar to Java and C# classes.
  • JavaScript also has classes, but TypeScript adds types.
  • Constructors are called when you create an object.
  • Static members are shared by all objects.

πŸ€” Did You Know?

  • Did you know that classes can be extended?
  • Did you know that TypeScript classes are compiled to JavaScript functions?
  • Did you know that private properties are a TypeScript feature?
  • Did you know that classes help you write reusable code?

🧠 Remember This

  • Classes are blueprints for objects.
  • Objects are instances of classes.
  • Properties are data; methods are actions.
  • Constructors initialise objects.
  • You are a class master.

⚠️ Common Mistakes

  • Mistake 1: Forgetting to use the new keyword.
  • Mistake 2: Forgetting to specify types for properties.
  • Mistake 3: Using private when you need public.
  • Mistake 4: Not using constructors.
  • Mistake 5: Forgetting the this keyword.

βœ… Best Practices

  • Always use the new keyword to create objects.
  • Always specify types for properties.
  • Always use constructors to initialise objects.
  • Always use the this keyword to refer to properties.
  • Always use access modifiers to control visibility.

🎨 ASCII Illustrations

Class – Example

   class Car {
     brand: string;
     model: string;
     constructor(brand: string, model: string) {
       this.brand = brand;
       this.model = model;
     }
     start(): void {
       console.log("Car started");
     }
   }

Object – Example

   let myCar = new Car("Toyota", "Camry");
   console.log(myCar.brand); // Toyota
   myCar.start(); // Car started

Comparison: Class vs Object

   +-----------------------+-----------------------+
   |  Class                |  Object               |
   +-----------------------+-----------------------+
   | Blueprint             | Instance              |
   | Template              | Real thing            |
   | Defines structure     | Holds data            |
   | Not usable directly   | Usable                |
   | One class             | Many objects          |
   +-----------------------+-----------------------+

πŸ“Š Comparison Tables

Access Modifiers

ModifierAccessible In
publicEverywhere
privateOnly the class
protectedClass and subclasses

Class Components

ComponentDescriptionExample
PropertyDatabrand: string
MethodActionstart(): void
ConstructorInitialisationconstructor()
StaticClass-levelstatic brand: string

πŸ“ Summary After Every Lesson

(Included after each lesson above – review them!)


πŸ“Œ End-of-Module Summary

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!


❓ Frequently Asked Questions

  1. What is a class? A blueprint for objects.
  2. What is an object? An instance of a class.
  3. What is a property? Data in a class.
  4. What is a method? An action in a class.
  5. What is a constructor? Initialises an object.
  6. What is this? Refers to the current object.
  7. What is public? Accessible anywhere.
  8. What is private? Accessible only in the class.
  9. What is protected? Accessible in subclasses.
  10. What is static? Belongs to the class.

πŸ“ Review Questions (15)

  1. What is a class?
  2. What is an object?
  3. What is a property?
  4. What is a method?
  5. What is a constructor?
  6. What is the this keyword?
  7. What is public?
  8. What is private?
  9. What is protected?
  10. What is static?
  11. How do you create a class?
  12. How do you create an object?
  13. What is a common mistake?
  14. What is a best practice?
  15. What makes you a class master?

✏️ Fill-in-the-Blank Exercises

  1. A __________ is a blueprint for objects.
  2. An __________ is an instance of a class.
  3. A __________ is data in a class.
  4. A __________ is an action in a class.
  5. A __________ initialises an object.
  6. The __________ keyword refers to the current object.
  7. __________ members are accessible anywhere.
  8. __________ members are accessible only in the class.
  9. __________ members are accessible in subclasses.
  10. You are a __________ master.

βœ… True or False Exercises

  1. A class is a blueprint for objects. (True)
  2. An object is a blueprint for classes. (False)
  3. A property is data in a class. (True)
  4. A method is an action in a class. (True)
  5. A constructor initialises an object. (True)
  6. this refers to the current object. (True)
  7. Public members are accessible only in the class. (False)
  8. Private members are accessible anywhere. (False)
  9. Protected members are accessible in subclasses. (True)
  10. You are a class master. (True)

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

  1. What is a class?
    a) A blueprint for objects
    b) An instance of an object
    c) A game
    Answer: a
  2. What is an object?
    a) An instance of a class
    b) A blueprint for classes
    c) A game
    Answer: a
  3. What is a property?
    a) Data in a class
    b) An action in a class
    c) A game
    Answer: a
  4. What is a method?
    a) An action in a class
    b) Data in a class
    c) A game
    Answer: a
  5. What is a constructor?
    a) Initialises an object
    b) Deletes an object
    c) A game
    Answer: a
  6. What is this?
    a) Refers to the current object
    b) Refers to the class
    c) A game
    Answer: a
  7. What is public?
    a) Accessible anywhere
    b) Accessible only in the class
    c) A game
    Answer: a
  8. What is private?
    a) Accessible only in the class
    b) Accessible anywhere
    c) A game
    Answer: a
  9. What is protected?
    a) Accessible in subclasses
    b) Accessible anywhere
    c) A game
    Answer: a
  10. What is static?
    a) Belongs to the class
    b) Belongs to the object
    c) A game
    Answer: a
  11. What is a common mistake?
    a) Forgetting to use new
    b) Using new
    c) Creating objects
    Answer: a
  12. What is a best practice?
    a) Use constructors
    b) Ignore constructors
    c) Use private everywhere
    Answer: a
  13. What is the purpose of a class?
    a) To create objects
    b) To slow down code
    c) To confuse developers
    Answer: a
  14. Can you create multiple objects from one class?
    a) Yes
    b) No
    Answer: a
  15. Are you a class master?
    a) Yes
    b) No
    Answer: a

πŸ”— Matching Exercises

Match the term with its definition.

TermDefinition
1. ClassA. Blueprint for objects
2. ObjectB. Instance of a class
3. PropertyC. Data in a class
4. MethodD. Action in a class
5. ConstructorE. Initialises an object

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


πŸ“ Short Answer Questions

  1. What is a class and why is it useful?
  2. What is the difference between a class and an object?
  3. What is a constructor and when is it called?
  4. What is the difference between public, private, and protected?
  5. What is a static member and when would you use it?

🎭 Scenario-based Exercises

  1. Scenario 1: You need to create a class for a customer. What properties and methods would you include?
  2. Scenario 2: You need to create a class for a product. What properties and methods would you include?
  3. Scenario 3: You need to create a class for a student. What properties and methods would you include?

πŸ‘₯ Group Activity

β€œ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.


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

β€œ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.


πŸ—£οΈ Classroom Discussion Questions

  1. Why are classes useful?
  2. What is the difference between a class and an interface?
  3. When would you use a private property?
  4. When would you use a static method?
  5. What would you like to learn next?

πŸ› οΈ Mini Project

β€œ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.


πŸ’» Practical Assignment

β€œ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.


⚑ Challenge Exercise

β€œ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.


πŸ“‹ Quiz Answers

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


🎁 Key Takeaways

  • Classes are blueprints for objects.
  • Objects are instances of classes.
  • Properties are data; methods are actions.
  • Constructors initialise objects.
  • You are a class master.

πŸš€ Preparation for Module Six

In Module Six, you will learn about inheritance and polymorphism. To prepare:

  • Review what you learned about classes.
  • Think about how classes can be related to each other.
  • Bring your questions to the next class.

Get ready to learn about inheritance!


End of Module Five Β· You are now a class 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.
β†’