Before We Begin

In the previous section, we learned that Object-Oriented Programming (OOP) is a programming paradigm. Now it’s time to see how this idea is applied in practice.

At this stage, don’t try to memorize the code. Your goal is to understand the concept. Once the concept becomes clear, writing the code will feel much more natural.

Running the Examples

From this point on, you’ll start writing and experimenting with small code examples.

There’s no need to install a programming language or configure a development environment yet. You can run every example directly in an online editor, such as Programiz JavaScript Online Compiler.

Simply copy the code, run it, and focus on understanding what happens instead of memorizing the syntax.

Remember the Core Idea

Before continuing, always keep this idea in mind:

A Class is simply a blueprint, and from that blueprint we can create one or more Objects.

Here’s the example from the previous lesson:

// Model
class Car {
constructor() {
console.log("This is car model");
}
}
// Object
const car = new Car();

We created a blueprint called Car, then created an object from it using new.

Adding Attributes

So far, every car created from this class would be exactly the same because the class doesn’t store any information.

To make each object unique, we can give the class attributes.

// Model
class Car {
constructor(model) {
this.model = model;
console.log("This is car model " + model);
}
}
// Object
const car = new Car(2020);

Now the class accepts a value called model and stores it inside each object.

This allows us to create multiple objects from the same class while giving each one different data.

For example:

const car1 = new Car(2020);
const car2 = new Car(2023);
const car3 = new Car(2025);

All three objects are created from the same class, but each contains its own data.

Why Do We Use Attributes?

Attributes make objects flexible.

Instead of creating a new class for every car, we create a single reusable class and customize each object by providing different values.

This is one of the key ideas behind Object-Oriented Programming.

Key Takeaways

  • A Class is a blueprint used to create objects.
  • An Object is an instance created from a class.
  • Attributes allow each object to store its own data.
  • Focus on understanding the concept rather than memorizing the code. Practice will make the syntax feel natural.