# Class vs Object

> 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.

Canonical: https://www.internhack.in/learn/interview/oop-interview/oop-class-vs-object
Last updated: 2026-11-28

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

- A class is a blueprint (compile-time definition); an object is a runtime instance on the heap.
- A reference variable holds the address of an object, it is not the object itself.
- The == operator on references compares identity, not field values; use equals() for logical equality.
- Each object gets its own copy of instance fields but all objects share the class methods and static fields.

## Common follow-up questions

- Where are objects and references stored in memory?
- What is the difference between == and equals()?
- In how many ways can you create an object in Java?
- Can a class exist without any object being created?

## Interview tips

- Always use an analogy first (blueprint and house), then get technical; it shows you can teach.
- Do not say "an object is a variable of a class" without mentioning heap allocation and references.
- Draw the stack and heap on the whiteboard for the Car b = a example; interviewers love this.

## Concepts covered

class, object, instance, heap, reference

Next: [Four Pillars of OOP](https://www.internhack.in/learn/interview/oop-interview/oop-four-pillars) · [All of OOP Concepts](https://www.internhack.in/learn/interview/oop-interview)

## More OOP Concepts questions

- [Encapsulation and Data Hiding](https://www.internhack.in/learn/interview/oop-interview/oop-encapsulation)
- [Abstraction: Hiding Complexity](https://www.internhack.in/learn/interview/oop-interview/oop-abstraction)
- [Inheritance and Its Types](https://www.internhack.in/learn/interview/oop-interview/oop-inheritance-types)
- [Polymorphism: Compile-time vs Run-time](https://www.internhack.in/learn/interview/oop-interview/oop-polymorphism)
- [Constructors and Destructors](https://www.internhack.in/learn/interview/oop-interview/oop-constructors-destructors)
- [Method Overloading vs Overriding](https://www.internhack.in/learn/interview/oop-interview/oop-overloading-vs-overriding)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [OOP Concepts](https://www.internhack.in/learn/interview/oop-interview)
