What Is a Class?
A class is the description of a kind of object. It says what data each object will carry and what the object can do. The class itself is not an object. It is the plan the program follows every time it creates one.
So a class holds two things. The first is data, which we call attributes: a library member, for example, has an id, a name and a count of books borrowed. The second is behavior, which we call methods: the same member can answer canBorrow(), change its name with setName(), or be switched off with deactivate().
Premium Video
This video is available to Premium members or users who have purchased the course.
Sign in to UnlockA train reservation form
Picture the blank reservation form at a railway counter. It decides which boxes exist (name, age, train number, berth preference) and prints the rules for filling it in. A blank form books nobody a seat. The moment a passenger fills one in, you have something real: same boxes, but this form holds Ravi's name and Ravi's train.
The blank form is the class. The boxes are its attributes, the printed rules are its methods, and every filled-in form is an object.
We will use one example all the way through this article: a small library system. Here is its Member class drawn as a UML class diagram. The name sits on top, the attributes in the middle (a minus sign means private) and the methods at the bottom (a plus sign means public).
UML class diagram of Member: private attributes on top, public methods below
In a design problem, the main things the problem talks about usually turn into classes. For a library that means Member, Book, Loan and maybe Fine, each one a separate class that works with the others.
What Is an Object?
An object is one instance of a class: an actual thing in memory, built from the plan while the program runs. You put data into it and call its methods.
Two objects made from the same class have the same shape. They have the same fields and the same methods. But each one keeps its own values, and changing one leaves the other alone.
Here is a small ParkingSlot class with two objects. Parking a car in slot1 does nothing to slot2.
Expected output
Slot 1: occupied
Slot 2: free
One ParkingSlot class, two objects: each object keeps its own data
One class, two objects, two separate sets of data.
Keep this difference in mind for the rest of the article. You write a class once, in your source code. Objects are created from it at run time, as many as you need.
Why Classes Matter in LLD
A class gives a group of related data and behavior one clear name. That alone makes code easier to read. It also lets you hide the inside of an object from the rest of the program (encapsulation), and it lets you create as many objects as you like from one definition.
Most LLD interview questions, from a parking lot to an elevator or a library, start with the same step: decide which classes you need and what each one is responsible for. If that step goes well, the rest of the design usually follows.
How to Think About a Class in LLD
A good class stands for one real idea from the problem. When you are working out what that class should be, these four questions help:
What is the thing? Look for the ideas the problem keeps mentioning: Member, Book, Ticket, Vehicle, ParkingSlot.
What does it need to remember? An id, a title, a status, a due date.
What can it do, or what can be done to it? borrowBy(), returnBook(), markLost(), canBorrow().
Put that data and those methods together under one class name.
From a problem statement to a class in four steps
A simple trick is to underline the nouns in the problem statement ("a member borrows a book"). They are your candidate classes. The verbs ("borrows", "returns") are your candidate methods. How classes connect to each other comes later. For now, get comfortable with one class at a time.
Basic Structure of a Class
Here is the skeleton of a class. The explanation below follows the Java tab; the other tabs show the same skeleton in C++ and Python if you want to compare.
1. The class declaration
In Java you write public class ClassName { ... }. A public class has to live in a file with the same name, so Member goes in Member.java. Give the class a plain noun that says what it is: Member, Book, Ticket.
2. Attributes
Attributes are the variables every object carries around. For our library member they look like this:
Java wants each attribute declared with its type inside the class body. Every Member object gets its own copy, so Ravi's borrowedCount and Priya's borrowedCount are two different numbers.
3. Public and private
Access modifiers decide who may touch a field or a method. A private member can only be used by code inside the class. A public member can be used by anyone, which is what you want for the methods other classes call. In Java you put private or public in front of each member.
The usual rule is simple: keep fields private and offer public methods that control how the data is read and changed.
this.name means "the name field of this object". You need it here because the parameter has the same name. Notice that the setter checks the value before saving it, so no member can ever end up with an empty name.
4. The constructor
The constructor runs once, when the object is created, and its job is to leave the object in a valid starting state.
In Java the constructor has the same name as the class and no return type, and you call it with new. Here every new member starts with borrowedCount at 0 and active set to true, so no caller can forget either one.
Creating objects then looks like this:
Each call to new gives you a separate object with its own data.
Writing Methods Inside a Class
Methods are what an object can do. Here is the full Member class: the fields, constructor and getters from above, plus the methods the library actually needs.
canBorrow() answers a business question from the object's own data: a member may borrow if they are active and hold fewer than MAX_BOOKS (3) books. recordBorrow() and recordReturn() keep the count right, and recordReturn() never lets it drop below zero.
MAX_BOOKS is static. It belongs to the class rather than to each object, so all members share one value.
Using the class:
Expected output
Name cannot be empty
Ravi Kumar can borrow: yes
Ravi Kumar can borrow: no
The calling code never touches active or borrowedCount. It asks the object a question or tells it to do something, and the object keeps its own data consistent.
A More LLD‑Style Example: Book Borrowed by a Member
Now we can put two classes together. A member borrows a book, so we need Member (the class we just wrote) and a new Book class that remembers who has it.
Book keeps a reference to the Member who borrowed it and uses the BookStatus enum
A book follows three rules. borrowBy(member) only works when the book is AVAILABLE and the member is allowed to borrow (active and under the limit). returnBook() only works when the book is BORROWED. markLost() works from either state, and once a book is LOST it stays lost.
Book status rules: AVAILABLE and BORROWED switch back and forth; LOST is final
Here is the Book class, using the Member class from the previous section:
A few things are worth pointing out. The Java enum prints its own name, so the status shows up as LOST rather than a number. The book keeps a reference to the Member object, not a copy, so when it calls borrower.recordBorrow() the real member's count goes up. In Java every variable of a class type is already a reference, so you get this for free.
The work is also split sensibly. Member decides whether it is allowed to borrow; Book decides whether it can be borrowed. Neither class reaches into the other's private fields.
Linking the objects together:
Expected output
Clean Code borrowed by Ravi Kumar
Clean Code cannot be borrowed (status: BORROWED)
Clean Code returned by Ravi Kumar
Clean Code borrowed by Priya Sharma
Clean Code marked as lost
Clean Code cannot be returned (status: LOST)
Priya holds 0 book(s)
Why this design holds up
A book's status and its borrower live together inside the Book object, not in some separate map. Because callers cannot set the status directly, a lost book can never be borrowed again and two members can never hold the same copy. Each class looks after one thing: the member tracks its limit, the book tracks its status. And when the library later wants due dates, fines or a waiting list, you add fields and methods to these classes instead of rewriting every caller.
Common Mistakes When Writing Classes in LLD
These are the mistakes that show up again and again when people start designing with classes.
1. Making every field public
If any code can write any value, nothing protects the object from ending up in a nonsense state. Compare the two versions:
Keep the fields private and let methods enforce the rules.
2. One class that does everything
A single Library class that handles members, books, fines and email notifications gets hard to read and harder to change. Split it: Member, Book, FineCalculator, NotificationService.
3. Half-built objects
Creating an object and filling in the required fields "later" leaves a window where the object is incomplete. Pass the required data to the constructor, so every Book has an ISBN and a title from the first moment.
4. Making a copy when you meant to share
Writing new Member(ravi.getId(), ravi.getName(), ...) gives you a second, separate member. Updating it does nothing to Ravi. When two objects need to see the same data, pass the existing reference around.
5. Vague names
Manager, Handler and Data tell the reader nothing. Book, ParkingSlot and TicketBooking tell them exactly what they are looking at.
Summary
A class describes the data and behavior of one kind of object. An object is one instance of that class, with its own values. In LLD, classes are how you model the things in the problem: Member, Book, Ticket, ParkingSlot.
In this article you turned a problem statement into classes by picking out the nouns and verbs. You built a Java class with private fields, a constructor and public methods, and you saw why the data stays private. Finally, you connected two classes, a Book that guards its own status and a Member that guards its own borrowing limit.
Next up is how classes relate to each other: association, composition and inheritance.