Classes and Objects
Model real things with classes, constructors, encapsulation and records.
What you’ll learn
- The difference between a class and an object
- Fields, constructors and methods
- Encapsulation with private fields
- When to use a record instead of a class
Class vs object
A class is a blueprint. An object is a thing built from that blueprint. Employee is a class; "Asha, backend engineer, ₹18 LPA" is an object.
public class Employee {
// Fields: the state each object carries
private final String name;
private double salary;
// Constructor: how a new object is created
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
// Methods: what the object can do
public void raise(double percent) {
salary += salary * percent / 100;
}
public String getName() { return name; }
public double getSalary() { return salary; }
}Employee asha = new Employee("Asha", 1_800_000);
asha.raise(10);
System.out.println(asha.getSalary()); // 1980000.0new allocates the object on the heap and runs the constructor. this.name refers to the field; plain name refers to the constructor parameter.
Encapsulation
Notice the fields are private. Outside code can't write asha.salary = -5. It must go through methods, which lets the class protect its own rules:
public void raise(double percent) {
if (percent <= 0) throw new IllegalArgumentException("Raise must be positive");
salary += salary * percent / 100;
}This is encapsulation: hide the data, expose behaviour. It's the single most useful OOP idea in day-to-day backend code.
Tip
Make fields final whenever they shouldn't change after construction. Immutable objects are easier to reason about and safe to share between threads.
Static members
static members belong to the class, not to any object:
public class Employee {
private static int headcount = 0;
public Employee(String name, double salary) {
// ...
headcount++;
}
public static int getHeadcount() { return headcount; }
}Records: data classes in one line
A lot of classes just carry data — API responses, DTOs, config values. Since Java 16, records generate the constructor, getters, equals, hashCode and toString for you:
public record Point(int x, int y) {}
var p = new Point(3, 4);
p.x(); // 3
p.equals(new Point(3, 4)); // trueRecords are immutable and are perfect for Spring Boot request/response bodies.
⚡ Try it yourself
Create a BankAccount class with a private balance, a deposit(amount) method and a withdraw(amount) method that refuses to let the balance go below zero.
Interview questions
QWhat is encapsulation and why does it matter?+
Encapsulation is bundling data with the methods that operate on it and restricting direct access to the data (usually with private fields). It lets a class enforce its invariants and change its internal representation without breaking callers.
QWhen would you use a record instead of a class?+
When the type is a transparent, immutable carrier of data — DTOs, value objects, keys in maps. Use a regular class when you need mutable state, inheritance, or to hide the internal representation.
Key takeaways
- A class is a blueprint;
newcreates objects from it. - Keep fields
privateand expose behaviour through methods. - Use
recordfor simple immutable data carriers.
Finished this lesson?
Mark it complete to move forward on your roadmap.