What is object-oriented programming, and what makes a design object-oriented rather than procedural code that happens to live in classes?
Object-oriented programming organises a program around objects: bundles of data (state) together with the operations allowed to touch that data (behaviour). Rather than a program being a pile of functions that receive data, the data becomes the thing that answers questions about itself. A bank account that knows how to deposit, withdraw and report its balance is the canonical example, instead of scattered helpers each editing account fields directly.
Writing classes is not the same as writing object-oriented code, and that distinction is usually what an interviewer is probing. Procedural logic can be packaged inside classes and still pass data around as loose parameter lists. The object-oriented discipline is that objects communicate through a small public surface and each object guards its own state so no outsider can corrupt it.
public class Main {
public static void main(String[] args) {
Cart cart = new Cart();
cart.add(new Item("Keyboard", 79.0));
cart.add(new Item("Mouse", 25.0));
System.out.println(cart.describe());
}
}
class Cart {
private final java.util.List<Item> items = new java.util.ArrayList<>();
void add(Item item) { items.add(item); }
String describe() { return items.size() + " item(s): " + items; }
}
class Item {
private final String name;
private final double price;
Item(String name, double price) { this.name = name; this.price = price; }
public String toString() { return name + " $" + price; }
}Read the code for the discipline it models. The cart owns its list of items, so the only way to change that list is the add method and the only way to learn what is inside is describe. The item prints itself instead of the cart reaching into private fields to assemble a string. State is owned, behaviour is exposed, and internals stay free to change without breaking callers.
Be honest about the trade-off. Objects cost more to design and read than a flat function, so the style earns its keep when behaviour varies, state must stay consistent, or several parts of the system share the same domain logic. For a one-off calculation, a plain function is usually the better answer.