Class vs Object

Difficulty: Beginner

Question

What is the difference between a class and an object? Explain with an example, and tell me what happens in memory when you write Car b = a.

Answer

Let's start with the most basic question in any OOP round, because how cleanly you answer it tells the interviewer a lot about how solid your fundamentals are.

Think of a class as the architectural blueprint of a house. The blueprint says how many rooms there are, where the doors go, and what the house can do. But you cannot live in a blueprint. An object is an actual house built from that blueprint. You can build a hundred houses from one blueprint, each with its own paint colour and its own family living inside, but they all share the same structure.

Technically, a class is a user-defined type that bundles data (fields, also called attributes or state) and behaviour (methods). It exists at compile time as a definition, and the JVM loads it once into the method area (metaspace in modern Java). An object is a runtime instance of that class. It is allocated on the heap, has its own copy of the instance fields, and has an identity, meaning two objects with identical field values are still two different objects.

The part that trips up freshers is the reference. When you write Car a = new Car("Tata"), three things happen. The new operator allocates memory on the heap and runs the constructor. The variable a, which lives on the stack, stores a reference (think of it as an address) to that heap object. So a is not the object, it is a remote control pointing at the object. When you then write Car b = a, you do not create a second car. You create a second remote control for the same car. Anything done through b is visible through a. That is why a == b is true, and why a == c is false when c was created with a separate new, even if both have the same brand.

Some more differences worth mentioning. A class does not occupy memory for instance data until you create objects; objects consume heap memory proportional to their fields. A class is declared once; objects are created many times. Static members belong to the class itself and are shared, while instance members belong to each object. In Java, objects are always created dynamically with new (or through reflection, deserialization, or clone), whereas in C++ you can also create objects on the stack without new, and their lifetime is then tied to the scope.

Also remember that not every class is meant to be instantiated. Abstract classes and interfaces cannot be turned into objects directly, and a class with a private constructor, such as a utility class or singleton, controls who can create objects.

A common follow-up is: how many ways can you create an object in Java? The answer is five: the new keyword, Class.newInstance or Constructor.newInstance via reflection, clone(), deserialization, and factory methods that wrap one of these. If you can also mention that the garbage collector reclaims an object once no reachable reference points to it, you will sound very well prepared.

Code examples

Two references, one object

public class ClassVsObject {
    static class Car {
        String brand;
        int speed;
        Car(String brand) { this.brand = brand; }
        void accelerate() { speed += 10; }
    }

    public static void main(String[] args) {
        Car a = new Car("Tata");
        Car b = a;              // second reference, same object
        Car c = new Car("Tata"); // brand new object

        a.accelerate();
        System.out.println(b.speed);
        System.out.println(c.speed);
        System.out.println(a == b);
        System.out.println(a == c);
    }
}

b points to the same heap object as a, so the speed change is visible through b. c is a different object with its own speed field.

Key points

Concepts covered

class, object, instance, heap, reference