A class is a blueprint or template that defines what data (properties) and behavior (methods) its objects will have — it doesn't itself hold any real data. Think of a class like an architect's blueprint for a house: the blueprint defines that every house built from it will have a certain number of rooms, doors, and windows, but the blueprint itself is not a house you can live in.
class Car {
// Properties (state) — every Car will have these
brand
model
speed = 0
// Methods (behavior) — every Car can do these
function accelerate(amount) {
this.speed += amount
}
function brake(amount) {
this.speed = max(0, this.speed - amount)
}
}
A class typically describes:
- Properties / attributes / fields — the data each object of this type will hold
- Methods — the functions/behaviors each object can perform
- Constructors — special methods that set up a new object's initial state (covered in a later chapter)
Classes are the fundamental organizing unit of OOP code: rather than scattering related data and logic across a program, a class groups everything relevant to one concept — a Car, a User, an Order — into a single, cohesive definition.