Mock Teaching Session
Thinking in Objects, Not Just Code
Today, we shift from writing instructions line by line to designing software as a set of meaningful objects working together.
Foundation
OOP organizes software around objects: units that combine data with behavior.
Core Idea
A program becomes a collection of objects that represent real-world concepts and collaborate through behavior.
Class and Object
Blueprint or template for creating objects.
class Car {
constructor(brand) {
this.brand = brand;
}
}
const myCar = new Car("Myvi");
The Four Pillars
Keep data and methods together behind controlled access.
Expose only what another object needs to know.
Build a child class from a parent class.
Use the same method name with different outcomes.
Encapsulation Analogy
You do not directly manipulate the engine, fuel injection, or transmission. The manufacturer gives you safe controls.
Class Checkpoint
Question for the class: which operation should be allowed to change the car's speed?
Speed starts at 0. Choose one operation with the class.
Encapsulation in OOP
class Car {
private int speed = 0;
public void accelerate() {
speed += 10;
}
}
car.speed = 500;
car.accelerate();
Abstraction
Abstraction hides unnecessary internal details and shows only what the user needs.
Two More Pillars
Start with a shared parent idea, then let each child object respond in its own way.
Code Example
class Animal {
speak() {
console.log("Animal makes a sound");
}
}
class Dog extends Animal {
speak() {
console.log("Dog barks");
}
}
class Cat extends Animal {
speak() {
console.log("Cat meows");
}
}
The caller uses the same method name. The object decides the behavior.
Why It Matters
Key Takeaways
OOP designs software around objects.
Classes are blueprints; objects are real instances.
The four pillars help us write cleaner and reusable programs.
Enable this when you want students to follow your slide.