Advanced Level • Lesson 1

🏗️ Object-Oriented Programming

⏱️ 40 minutes 📚 Advanced 🎯 OOP Concepts
🏗️

Welcome to Object-Oriented Programming!

Learn to organize code like real-world objects - cars, students, and more!

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a way to organize code by modeling real-world things as "objects". Think of it like creating blueprints for Malaysian cars - Proton, Perodua, motorcycles - each has properties (color, brand) and actions (start, stop, honk)!

Instead of writing separate code for every car, we create a class (blueprint) and then make many objects (actual cars) from it.

💡 Real-World Example: A Perodua Myvi is an object. All Myvis share the same blueprint (class) but each has different properties - different colors, owners, license plates!

Key Concepts

📋

Class

A blueprint or template for creating objects. Like a car design plan.

🚗

Object

An instance created from a class. Like an actual car built from the plan.

🏷️

Properties

Characteristics of an object. Like color, brand, or speed.

⚙️

Methods

Actions an object can do. Like start(), stop(), or honk().

🔨

Constructor

Special method that runs when creating a new object. Sets initial values.

🧬

Inheritance

When one class inherits properties from another. Like a motorcycle inheriting from Vehicle.

Creating Your First Class

🚗 Example: Vehicle Class

Let's create a Vehicle class for Malaysian cars:

class Vehicle { constructor(brand, color, year) { this.brand = brand; this.color = color; this.year = year; this.isRunning = false; } start() { this.isRunning = true; console.log(this.brand + " is starting... Vroom!"); } stop() { this.isRunning = false; console.log(this.brand + " has stopped."); } honk() { console.log(this.brand + " says: Beep beep!"); } getInfo() { return this.year + " " + this.color + " " + this.brand; } } // Create objects (actual vehicles) let myProton = new Vehicle("Proton X70", "Blue", 2023); let myPerodua = new Vehicle("Perodua Myvi", "Red", 2022); myProton.start(); // "Proton X70 is starting... Vroom!" myProton.honk(); // "Proton X70 says: Beep beep!" console.log(myPerodua.getInfo()); // "2022 Red Perodua Myvi"

What's happening? We created a Vehicle blueprint, then made two actual vehicles (myProton and myPerodua) from it!

Constructor Method

The constructor is a special method that runs automatically when you create a new object. It sets up the initial properties.

🎓 Example: Student Class with Constructor

class Student { constructor(name, age, grade, school) { this.name = name; this.age = age; this.grade = grade; this.school = school; this.subjects = []; } introduce() { console.log("Hi! I'm " + this.name + " from " + this.school); } addSubject(subject) { this.subjects.push(subject); console.log(subject + " added to " + this.name + "'s subjects"); } getSubjects() { return this.subjects.join(", "); } } let student1 = new Student("Ahmad", 16, "Form 4", "SMK Bandar Utama"); student1.introduce(); // "Hi! I'm Ahmad from SMK Bandar Utama" student1.addSubject("Mathematics"); student1.addSubject("Physics"); console.log(student1.getSubjects()); // "Mathematics, Physics"

Inheritance - Extending Classes

Inheritance allows one class to inherit properties and methods from another. This helps avoid repeating code!

🏍️ Example: Motorcycle Inherits from Vehicle

class Motorcycle extends Vehicle { constructor(brand, color, year, type) { super(brand, color, year); // Call parent constructor this.type = type; // New property specific to motorcycles } wheelie() { console.log(this.brand + " is doing a wheelie! 🏍️"); } // Override the honk method honk() { console.log(this.brand + " says: Beep beep beep! (motorcycle horn)"); } } let myMotorcycle = new Motorcycle("Honda Wave", "Black", 2023, "Sport"); myMotorcycle.start(); // Inherited from Vehicle myMotorcycle.wheelie(); // New method for Motorcycle myMotorcycle.honk(); // Overridden method

Key Points: Motorcycle inherits all Vehicle methods but can add new ones (wheelie) and override existing ones (honk)!

Properties vs Methods

Remember:
  • Properties = Data/characteristics (brand, color, age)
  • Methods = Actions/functions (start(), stop(), introduce())
  • this keyword = Refers to the current object
  • constructor = Runs once when object is created

Common Mistakes to Avoid

⚠️ Watch Out For:

  • Forgetting 'new': Always use "new Vehicle()" not just "Vehicle()"
  • Missing 'this': Inside methods, use "this.property" not just "property"
  • Wrong super(): In child classes, call super() before using 'this'
  • Overwriting methods: Be careful when overriding parent methods

Summary

You've learned:

  • ✅ Classes are blueprints, objects are instances created from them
  • ✅ Constructor sets up initial properties when creating objects
  • ✅ Methods are functions that belong to a class
  • ✅ Properties store data about an object
  • ✅ Inheritance lets classes share code (extends keyword)
  • ✅ Use 'this' to refer to current object, 'super' for parent class

🎮 Interactive Challenge: Create Your Own Classes!

📱 Challenge 1: Phone Class

Create a Phone class with properties (brand, model, battery) and methods (call, charge, useApp):

🍔 Challenge 2: Restaurant Class

Create a Restaurant class for Malaysian restaurants (mamak, kopitiam, etc.):

💡 Tip: Think about what properties and methods make sense. A restaurant should have a menu, take orders, serve food, etc.!

🎯 Activity: Design a Class System

What You'll Do:

Design a complete class system for a Malaysian school!

Instructions:

  1. Create a base Person class with name, age, IC number
  2. Create Student class that extends Person (add grade, subjects)
  3. Create Teacher class that extends Person (add subject taught, years of experience)
  4. Add relevant methods to each class
  5. Create at least 2 students and 2 teachers
  6. Make them interact (teacher teaches student, etc.)

Bonus Challenges:

  • Add a Classroom class that can contain students and a teacher
  • Add methods for taking attendance, giving grades
  • Create a subject assignment system
🏆 Challenge: Can you model a complete school day using your classes? Morning assembly, classes, recess, etc.?

💪 Practice Challenges

Challenge 1: Vehicle Management System

Create a system to manage Malaysian vehicles:

  • Base Vehicle class with common properties
  • Proton class (extends Vehicle) - add turbo boost feature
  • Perodua class (extends Vehicle) - add fuel efficiency tracker
  • Motorcycle class (extends Vehicle) - add helmet check method

Create 2 of each type and demonstrate all their features!

Challenge 2: Student Management System

Build a complete student management system:

  • Student class with name, age, grade, subjects array
  • Methods: addSubject(), removeSubject(), calculateGPA()
  • Method to display full student information
  • Create 5 students from different Malaysian schools
  • Add subjects and grades for each

Bonus: Add a method to find the top student by GPA!

Challenge 3: E-Commerce Product System

Create an object-oriented e-commerce system:

  • Product class (name, price, stock, category)
  • Methods: updateStock(), applyDiscount(), isAvailable()
  • ElectronicsProduct extends Product (add warranty period)
  • ClothingProduct extends Product (add sizes array)
  • Create products for Shopee/Lazada style marketplace

Challenge 4: Bank Account System

Design a simple banking system:

  • BankAccount class (accountNumber, name, balance)
  • Methods: deposit(), withdraw(), checkBalance(), transfer()
  • SavingsAccount extends BankAccount (add interest rate)
  • Create accounts for Maybank, CIMB, etc.
  • Implement proper validation (can't withdraw more than balance!)

Security: Make sure balance can't go negative!