🏗️ Object-Oriented Programming
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.
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:
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
Inheritance - Extending Classes
Inheritance allows one class to inherit properties and methods from another. This helps avoid repeating code!
🏍️ Example: Motorcycle Inherits from Vehicle
Key Points: Motorcycle inherits all Vehicle methods but can add new ones (wheelie) and override existing ones (honk)!
Properties vs Methods
- 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.):
🎯 Activity: Design a Class System
What You'll Do:
Design a complete class system for a Malaysian school!
Instructions:
- Create a base Person class with name, age, IC number
- Create Student class that extends Person (add grade, subjects)
- Create Teacher class that extends Person (add subject taught, years of experience)
- Add relevant methods to each class
- Create at least 2 students and 2 teachers
- 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
💪 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!