ODNYX.
❮ HomeNext: Working with Enums ❯

Object-Oriented Programming Interview Questions

Common Java interview questions about classes, objects, and the fundamentals of object-oriented programming.

Video Tutorial Coming Soon — Channel Production Underway

1. What is the difference between a class and an object?easy

A class is a blueprint or template that defines what fields and methods its instances will have — it doesn't hold real data by itself. An object is a specific instance created from that class, with its own actual values stored in memory.

public class Car {
    String color;
}

Car myCar = new Car();   // myCar is an object — a real instance of the Car class
myCar.color = "red";
2. What happens in memory when you write `new Car()`?medium

The JVM allocates memory on the heap large enough to hold the new object's fields, runs the class's constructor to initialize those fields, and returns a reference (memory address) to that newly created object. The variable you assign it to (like myCar) doesn't hold the object itself — it holds that reference.

3. Can two different object variables refer to the same underlying object?medium

Yes. Assigning one object variable to another copies the reference, not the object itself, so both variables end up pointing at the same object in memory.

Car a = new Car();
a.color = "red";

Car b = a;          // b now points to the same object as a
b.color = "blue";

System.out.println(a.color); // "blue" — because a and b are the same object
4. What is the purpose of a constructor?easy

A constructor initializes a newly created object's state (its fields) at the moment it's created. If you don't define one, Java automatically provides a no-argument default constructor that does nothing beyond basic field defaults (0, false, null, etc.).

5. What is polymorphism, and what are its two types in Java?easy

Polymorphism means "many forms" — the ability for the same method call to behave differently depending on context. Java has two kinds:

  • Compile-time (static) polymorphism — achieved through method overloading, resolved by the compiler.
  • Runtime (dynamic) polymorphism — achieved through method overriding, resolved by the JVM based on the object's actual type.
6. Can you overload a method by changing only its return type?medium

No. Method overloading requires a different parameter list (number, order, or type of parameters). Changing only the return type while keeping the same name and parameters causes a compile error — the compiler can't tell which method to call based on return type alone.

int calculate(int a, int b) { return a + b; }
// double calculate(int a, int b) { return a + b; } // ❌ compile error: duplicate method
7. What is the difference between method overloading and method overriding?medium
Overloading Overriding
Resolved by Compiler (compile-time) JVM (runtime)
Requires inheritance? No Yes
Method signature Must differ (parameters) Must be identical
Also called Static polymorphism Dynamic polymorphism
8. Why can't you override a static method in Java?hard

Static methods belong to the class itself, not to any instance, and are resolved at compile time based on the reference type — not the actual object type. Defining a static method with the same signature in a subclass hides the parent's version rather than overriding it; which one runs depends on the reference type used to call it, not the real object, which is the opposite of how overriding works.