Classes & Objects
A class describes a kind of thing: its data and the operations on that data. An object is one such thing, alive in memory, with its own state.
Overview
A class is a description. It names a kind of thing, lists the data each one carries (fields) and the operations it supports (methods). An object is one concrete thing built from that description. One BankAccount class can produce a million account objects; each has its own balance, and all of them share the same deposit and withdraw code.
This is where a low-level design interview starts. Before any pattern or principle, the interviewer wants to see that you can turn a sentence like 'a customer has accounts, and each account records its transactions' into a few well-named classes with clear fields, clear methods and clear ownership. Everything else on this page (inheritance, interfaces, encapsulation) is a rule about how those classes relate.
A class is the plan, an object is one thing built from it
The class is a type: the definition of fields and methods. You write it once, and it holds the method code, the constants and any class-level fields. It exists from the moment the program loads it.
An object is an instance, one value of that type. You create as many as you need, and each one holds its own copy of every instance field. It exists from construction until nothing refers to it any more. BankAccount is a class; Alice's checking account, with a balance of 240.00, is an object.
The diagram below draws both sides: the class with its definitions, and two account objects that hold nothing but their own values.
The symbols in front of each member are UML visibility marks, and Mermaid uses the same ones: + public, - private, # protected, ~ package. Draw them in interviews; they say who is allowed to touch what without a word of explanation.
Interview questions
- What is the difference between a class and an object?
- When should a member belong to the class rather than to each instance?
- What must a constructor guarantee?
- What happens when you assign one object variable to another?
- When should a class be an immutable value instead of an entity?
- Where do objects live in memory, and what does that mean for design?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio