Skip to main content

Command Palette

Search for a command to run...

Object-Oriented Programming – Notes (The Part Where Code Finally Starts Feeling Like Real Software)

Updated
9 min read

Okay I have to be honest. When my teacher first said "today we're doing Object-Oriented Programming" I panicked a little. The name sounds intimidating. Object-Oriented. Programming. Very official. Very scary. But after sitting through the explanation and actually writing some code I think this might be one of my favourite topics so far. It genuinely changes how you think about writing code. Let me break it down the way it made sense to me.


What Is Object-Oriented Programming?

So we already know what objects are from the last topic. Key-value pairs, properties, storing related data together. Good. Now OOP is a whole way of thinking about and structuring your code around the idea of objects.

The core idea is this – instead of writing a bunch of loose variables and functions floating around everywhere, you organise your code into self-contained units that represent real world things. Each unit has its own data and its own behaviour bundled together. Those units are objects.

And the way you create those objects in a structured repeatable way is through something called a class.


The Blueprint Analogy – This Is the One That Made It Click

Think about a car manufacturer. Before they build a single car they first design a blueprint. The blueprint says every car will have an engine, four wheels, a colour, a speed, and the ability to accelerate and brake. The blueprint itself is not a car. You can't drive a blueprint. But from that one blueprint they can manufacture thousands of actual cars. Each car is real, each one has its own specific colour and speed, but they all came from the same design.

In programming:

The blueprint is the class. The actual cars are the objects. Building a car from the blueprint is called instantiation – creating an instance of the class.

One class, many objects. That's the fundamental pattern of OOP. And the reason it's powerful is reusability. You define the structure once and then you can create as many objects as you need from it without rewriting anything.


Creating a Class in JavaScript

Here's what a basic class looks like:

class Car {

}

That's it. That's a class. Empty right now but valid. You use the class keyword followed by the name. Class names by convention always start with a capital letter. Car not car, Person not person, Student not student. This is just a naming convention but everyone follows it so you should too.


The Constructor Method

Now an empty class isn't very useful. We need to give our objects some properties when they're created. That's what the constructor is for.

The constructor is a special method that runs automatically every time you create a new object from the class. It's where you set up the initial properties.

class Car {
    constructor(brand, colour, speed) {
        this.brand = brand;
        this.colour = colour;
        this.speed = speed;
    }
}

Okay two things to explain here.

First – the parameters brand, colour, speed are the values you pass in when creating the object. Like arguments to a function.

Second – this. This is new and it confused me at first. this refers to the specific object being created. So this.brand means "this particular car's brand". It's the way the constructor assigns the values you pass in to the actual object's properties.

Without this, brand would just be a local variable inside the constructor and it would disappear the moment the constructor finished running. With this.brand it becomes a property that belongs to the object permanently.


Creating Objects From a Class

Now to actually create a car from our blueprint we use the new keyword:

let car1 = new Car("Toyota", "Red", 120);
let car2 = new Car("Honda", "Blue", 140);
let car3 = new Car("BMW", "Black", 200);

Each one of those lines runs the constructor, creates a brand new object, assigns the values you passed in, and stores it in the variable. Three objects, one class, no repeated structure code.

console.log(car1.brand);   // Toyota
console.log(car2.colour);  // Blue
console.log(car3.speed);   // 200

Each object has its own independent data. Changing car1 won't affect car2 or car3. They came from the same blueprint but they're completely separate boxes in memory.


Adding Methods Inside a Class

So far our objects have data. But real world things don't just have properties, they also do things. A car accelerates. A person speaks. A student studies. In OOP the behaviours of an object are called methods. And you define them right inside the class.

class Car {
    constructor(brand, colour, speed) {
        this.brand = brand;
        this.colour = colour;
        this.speed = speed;
    }

    describe() {
        console.log("This is a " + this.colour + " " + this.brand + " going at " + this.speed + " km/h");
    }

    accelerate(amount) {
        this.speed = this.speed + amount;
        console.log(this.brand + " sped up. New speed: " + this.speed + " km/h");
    }
}

Now every car object created from this class has access to describe() and accelerate() automatically.

let car1 = new Car("Toyota", "Red", 120);

car1.describe();          // This is a Red Toyota going at 120 km/h
car1.accelerate(30);      // Toyota sped up. New speed: 150 km/h
car1.describe();          // This is a Red Toyota going at 150 km/h

Notice how inside the methods we use this again to refer to the specific object the method is being called on. When car1.describe() runs, this refers to car1. When car2.describe() runs, this refers to car2. JavaScript handles that automatically.

This is where the power of classes really shows up. The method is written once but every object gets to use it with its own data.


Encapsulation – Bundling Data and Behaviour Together

Encapsulation is one of the main principles of OOP and it sounds fancy but the idea is honestly just what we've been doing.

Encapsulation means bundling the data (properties) and the behaviour (methods) that belong together into one unit (the class). Instead of having a car's brand floating in one variable, its speed in another variable, and an accelerate function written somewhere else entirely – everything that belongs to a car lives inside the Car class. It's all encapsulated in one place.

Think about a TV remote. You press the volume button and the volume goes up. You don't know exactly what circuit fires, what signal gets sent, what mechanism increases the volume. You just press the button and it works. The complexity is hidden inside the remote. You only interact with the buttons.

That's encapsulation. The internal workings are contained within the object. The outside world just calls the methods without needing to know the details of how they work.

In practice for where we are right now it mainly just means – keep related data and functions together inside a class rather than having everything scattered all over your code. Your code becomes cleaner, easier to read, and way easier to maintain.


Putting It All Together – A Person Example

Let me do one more clean example before the assignment because I think seeing it twice helps.

class Person {
    constructor(name, age, city) {
        this.name = name;
        this.age = age;
        this.city = city;
    }

    greet() {
        console.log("Hi, my name is " + this.name + " and I am " + this.age + " years old.");
    }

    introduce() {
        console.log("I live in " + this.city + ". Nice to meet you!");
    }
}

let person1 = new Person("Riya", 21, "Mumbai");
let person2 = new Person("Karan", 19, "Delhi");

person1.greet();       // Hi, my name is Riya and I am 21 years old.
person1.introduce();   // I live in Mumbai. Nice to meet you!

person2.greet();       // Hi, my name is Karan and I am 19 years old.
person2.introduce();   // I live in Delhi. Nice to meet you!

One class. Two completely independent objects. Each one has its own data. Each one can call the same methods but the methods work with that object's specific data. Clean, reusable, organised.


Assignment

Create a Student Class With Properties and a Method, Then Make Multiple Objects

class Student {
    constructor(name, age, course) {
        this.name = name;
        this.age = age;
        this.course = course;
    }

    printDetails() {
        console.log("Name: " + this.name);
        console.log("Age: " + this.age);
        console.log("Course: " + this.course);
        console.log("---");
    }
}

let student1 = new Student("Aryan", 17, "Computer Science");
let student2 = new Student("Priya", 18, "Data Science");
let student3 = new Student("Rohan", 17, "Web Development");

student1.printDetails();
student2.printDetails();
student3.printDetails();

Output:

Name: Aryan
Age: 17
Course: Computer Science
---
Name: Priya
Age: 18
Course: Data Science
---
Name: Rohan
Age: 17
Course: Web Development
---

Three student objects from one class. If I needed 30 students I wouldn't write any extra class code. Just 30 new Student(...) lines. That's the whole point.


Quick Recap

  • OOP is a way of organising code around objects that bundle data and behaviour together

  • A class is the blueprint – it defines the structure

  • An object is an instance of a class – the actual thing created from the blueprint

  • The constructor method runs when you create a new object and sets up its properties

  • this refers to the current object inside the class

  • Methods are functions defined inside a class – all objects of that class can use them

  • Encapsulation means keeping related data and behaviour together inside one class

  • Use the new keyword to create an object from a class

Honestly after doing variables, then objects, then classes – it feels like things are leveling up properly now. You can see how real applications are built. A user account is an object. A product in a shop is an object. A tweet is an object. Everything maps to this pattern.