Classes and Objects Interview Questions and Answers
Master Java Classes and Objects with production-ready interview questions covering class definition, object creation, constructors, instance variables, static members, memory allocation, object references, and enterprise Java examples.
Classes and Objects Interview Questions & Answers
Introduction
Everything in Object-Oriented Programming revolves around Classes and Objects.
Every Java application—from a simple console application to a large Spring Boot microservice—is built using classes and objects.
Examples include:
- Controller classes
- Service classes
- Repository classes
- Entity classes
- DTO classes
- Configuration classes
Understanding how classes and objects work internally is essential for writing maintainable Java applications and succeeding in technical interviews.
1. What is a Class?
Answer
A Class is a blueprint or template used to create objects.
It defines:
- Fields (State)
- Methods (Behavior)
- Constructors
- Nested Types
Example
public class Employee {
private String name;
public void work() {
System.out.println("Working");
}
}
A class itself does not occupy Heap memory for object data until objects are created.
2. What is an Object?
Answer
An Object is a runtime instance of a class.
Example
Employee employee =
new Employee();
Illustration
Employee Class
│
▼
new Employee()
│
▼
Employee Object
Objects contain actual data and execute methods defined in the class.
3. What is the difference between a Class and an Object?
Answer
| Class | Object |
|---|---|
| Blueprint | Runtime instance |
| Logical definition | Physical entity in memory |
| Defines structure | Holds actual data |
| Created once | Multiple objects can be created |
| No object data until instantiated | Occupies Heap memory |
A single class can create thousands of objects.
4. How is an object created in Java?
Answer
Objects are typically created using the new keyword.
Example
Employee employee =
new Employee();
Execution
Class Loaded
↓
Heap Memory Allocated
↓
Constructor Invoked
↓
Reference Returned
↓
Object Ready
The object resides in Heap memory, while the local reference is stored on the thread's Stack.
5. What happens internally when an object is created?
Answer
When the JVM executes:
Employee employee =
new Employee();
it performs the following steps:
- Verifies the class is loaded.
- Allocates Heap memory.
- Initializes fields with default values.
- Executes constructors.
- Returns the object reference.
Memory
Stack
employee
│
▼
Heap
Employee Object
6. What are instance variables?
Answer
Instance variables belong to individual objects.
Example
class Employee {
String name;
}
Each object maintains its own copy.
Employee e1 =
new Employee();
Employee e2 =
new Employee();
Memory
Heap
Employee1
name = "John"
-----------------
Employee2
name = "David"
Changing one object does not affect another.
7. What are static variables?
Answer
Static variables belong to the class rather than individual objects.
Example
class Employee {
static String company = "ABC";
}
Memory
Employee Class
↓
company = "ABC"
↓
Shared By All Objects
There is only one copy regardless of the number of objects created.
8. What is a Constructor?
Answer
A constructor initializes an object when it is created.
Example
class Employee {
Employee() {
System.out.println("Created");
}
}
Execution
new Employee()
↓
Constructor
↓
Object Initialized
Constructors have the same name as the class and do not declare a return type.
9. What is the difference between Constructors and Methods?
Answer
| Constructor | Method |
|---|---|
| Initializes objects | Performs operations |
| Same name as class | Any valid name |
| No return type | Has a return type or void |
| Invoked during object creation | Invoked explicitly |
Constructors prepare objects for use, while methods implement business logic.
10. Where are objects stored in memory?
Answer
Objects are stored in Heap Memory.
Example
Employee employee =
new Employee();
Memory
Stack
employee
│
▼
Heap
Employee Object
Local references reside in Stack memory.
11. Explain a production use case.
Answer
Scenario
A Spring Boot application receives a customer registration request.
HTTP Request
↓
CustomerDTO
↓
Customer Entity
↓
CustomerService
↓
CustomerRepository
↓
Database
Each request creates multiple objects:
- DTO
- Entity
- Response
- Validation objects
After request completion, temporary objects become eligible for Garbage Collection.
12. What are common mistakes related to Classes and Objects?
Answer
Common mistakes include:
Creating unnecessary objects.
Using static variables for instance-specific data.
Exposing internal fields directly.
Creating God classes with too many responsibilities.
Ignoring constructors for proper initialization.
Good object-oriented design keeps classes small and focused.
13. What are the best practices?
Answer
Recommended practices
- Design classes with a single responsibility.
- Keep fields private.
- Use constructors for mandatory initialization.
- Minimize mutable state.
- Prefer immutable objects where appropriate.
- Use static members only for shared data.
- Avoid creating unnecessary objects.
- Follow naming conventions consistently.
14. How are Classes and Objects used in Spring Boot?
Answer
Spring Boot applications consist of many specialized classes.
Controller
↓
Service
↓
Repository
↓
Entity
↓
DTO
The Spring IoC container manages many of these objects (beans), creating and injecting them where required.
Developers focus on object behavior while Spring manages object lifecycle.
15. What interview tips should you remember?
Answer
Interviewers commonly ask:
- Class
- Object
- Class vs Object
- Object creation
- Constructors
- Instance variables
- Static variables
- Heap vs Stack
- Spring Beans
- Production scenarios
Remember
- A class is a blueprint.
- An object is a runtime instance.
- Objects are created using
new. - Constructors initialize objects.
- Objects live in Heap memory.
- Local references live in Stack memory.
- Static variables are shared.
- Use real-world examples to explain classes and objects.
Summary
Classes define the structure and behavior of objects, while objects represent real runtime entities in memory. Understanding object creation, constructors, instance variables, static members, and JVM memory allocation is fundamental for building enterprise Java applications and answering OOP interview questions confidently.
Key Takeaways
- Understand what Classes and Objects are.
- Learn the difference between Classes and Objects.
- Know how objects are created.
- Understand constructors and initialization.
- Learn instance vs static variables.
- Understand Heap and Stack memory usage.
- Apply object-oriented design principles.
- Understand how Spring Boot manages objects.
- Follow enterprise Java best practices.
- Support interview answers with production examples.