Classes & Objects
EasyIn plain terms
A class is a template that describes what properties and methods its instances will have. An object is one concrete instance created from that class. For example, a "Person" class might define name and age and a greet() method; you then create many Person objects (Alice, Bob) with different names and ages.
Classes promote reuse: write the structure once, create many objects. They also give you a clear place to put related logic. In many languages, "new" creates an object from a class and runs a special constructor method to set up initial state.
What you need to know
- •Class = blueprint, Object = instance
- •Constructor sets initial state
- •Same class, many objects
Example
Code is language-agnostic in spirit; adapt the idea to your language:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return "Hi, I'm " + this.name;
}
}
const alice = new Person("Alice", 25);
const bob = new Person("Bob", 30);
alice.greet(); // "Hi, I'm Alice"Why this matters
Interviewers often ask you to design a class or explain the difference between class and object. Knowing constructor, instance vs static, and when to use a class vs a plain object matters.
How it connects
Classes are where encapsulation (next) is applied: private fields and public methods. Inheritance and polymorphism (later) extend classes. SOLID principles apply to how you design classes.
Interview focus
Be ready to explain these; they come up often.
- ▸Class = blueprint/template; object = instance with actual data.
- ▸Constructor: purpose (initialize state), when it runs (on new).
- ▸When to use a class vs a simple object: shared behavior, multiple instances, need for constructors.
Learn more
Dive deeper with these resources: