Classes and Objects: The Foundation of OOP
Before you can master Object-Oriented Programming, you need to clearly understand the relationship between a class and an object. Nearly every Java program you write will begin with these two concepts.
Many students initially assume that a class and an object are the same thing. They are closely related, but they are not identical. A class serves as a blueprint or template, while an object is the actual entity created from that blueprint.
What is a Class?
A class is a user-defined blueprint that describes what an object should contain. It defines the properties (data) and behaviors (methods) that every object created from it will have.
Think of a class as an architectural drawing of a house. The drawing specifies the number of rooms, doors, windows, and other details, but you cannot live inside the drawing itself. Similarly, a Java class only defines the structure—it does not represent a real object until an instance is created.
A class consumes very little memory because it simply contains the definition of an object. Memory for data is allocated only when objects are created.
What is an Object?
An object is a real instance of a class. It occupies memory, stores actual values, and can execute the methods defined in its class.
Unlike a class, which is only a template, an object represents something that actually exists while the program is running.
| Class | Object |
|---|---|
| Blueprint | Real instance |
| Defines structure | Stores actual values |
| No real data | Contains real data |
| Declared once | Can create many objects |
| No separate memory for variables | Memory allocated in Heap |
Understanding with a Real Example
Suppose a company manufactures laptops. Every laptop shares common characteristics such as brand, RAM, processor, and storage. Instead of describing every laptop individually, engineers first create a design specification.
In Java:
- Laptop specification → Class
- Individual laptop → Object
Thousands of laptops can be manufactured using the same design. Likewise, thousands of Java objects can be created from one class.
Creating Your First Class
class Car {
String brand;
String color;
void start() {
System.out.println("Car Started");
}
}
This class only describes what every Car object should contain. No memory is allocated for brand or color until an object is created.
Creating an Object
public class Main {
public static void main(String[] args) {
Car car1 = new Car();
car1.brand = "Toyota";
car1.color = "White";
car1.start();
}
}
Here, new Car() creates a new object. The variable car1 stores a reference to that object.
Creating Multiple Objects
One of the biggest advantages of OOP is that one class can create many independent objects.
class Student {
String name;
int age;
void display() {
System.out.println(name + " : " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.name = "Amit";
s1.age = 20;
s2.name = "Neha";
s2.age = 19;
s3.name = "Riya";
s3.age = 21;
s1.display();
s2.display();
s3.display();
}
}
Although all three objects belong to the same class, each stores its own data independently. Changing one object does not affect the others.
How Java Creates an Object Internally
When Java executes the following statement:
Student s1 = new Student();
Several steps occur behind the scenes:
- Java checks the Student class definition.
- Memory is allocated in the Heap.
- All instance variables receive default values.
- The object's constructor is executed.
- A reference to the object is returned.
- The reference is stored in variable s1.
Many beginners believe that s1 stores the actual object. It does not. It only stores the reference (address) of the object stored in Heap memory.
Where Are Objects Stored?
| Memory Area | Stores |
|---|---|
| Stack Memory | References and local variables |
| Heap Memory | Objects and instance variables |
| Method Area | Class information and bytecode |
You will study Java memory management in much greater detail later in the roadmap. For now, remember that objects live in the Heap, while reference variables generally reside in the Stack.
Why Is This Design Useful?
Separating blueprints from real objects makes software highly reusable. Instead of writing separate code for every employee, customer, or product, developers define one class and create as many objects as needed.
This design keeps programs cleaner, reduces duplication, and makes future enhancements much easier.
Real-World Usage of Classes and Objects
| Application | Possible Classes |
|---|---|
| Online Shopping | Customer, Product, Order, Cart |
| Banking System | Account, Customer, Loan, Transaction |
| Hospital | Doctor, Patient, Appointment |
| School Management | Student, Teacher, Course |
| Food Delivery | Restaurant, Menu, Order, DeliveryBoy |
If you later work with frameworks such as Spring Boot, you'll notice that almost every service, controller, entity, and repository is implemented as a class. Understanding classes and objects thoroughly now will make learning advanced Java much easier.
