Classes and Objects in Java
Classes and objects are the foundation of object-oriented programming. They help to structure real-world entities in our code base.
- Classes: Blueprint that creates objects. Shares common properties of the object.
- Objects: Specific entity that is created from a class.
Real Life Analogy: Building Blueprint
Class = Blueprint
A building blueprint defines:
- How many windows
- How many floors
- Type of materials
- Layout
But a blueprint is not a building — it's only the plan.
Object = Real Building
Objects are real buildings that are built using the blueprint.
✔ Class = Blueprint, Object = Real Building
Java Class (Blueprint)
The Building class is like a blueprint:
class Building {
int floors;
String color;
Building(int floors, String color) {
this.floors = floors;
this.color = color;
}
}Object = Real Building
Objects are created from the class using the new keyword.
Building building1 = new Building(5, "Blue");
Building building2 = new Building(10, "White");
System.out.println("This building has " + building1.floors + " floors and is " + building1.color + ".");
System.out.println("This building has " + building2.floors + " floors and is " + building2.color + ".");Output:
This building has 5 floors and is Blue.This building has 10 floors and is White.Ways to Create Objects
There are four main ways to create objects in Java:
1. Using new keyword
Most common way
2. Using Reflection
Create objects dynamically
3. Using clone() method
Copy existing objects
4. Using Deserialization
Recreate from byte stream
1. Using new Keyword
Most common way to create an object.
Building building1 = new Building(5, "Blue");
2. Using Reflection
Create an object without using the new keyword, by asking Java to create it at runtime using the class name.
Useful for:
- Frameworks (Spring, Hibernate)
- Loading classes dynamically
- Working with unknown classes at runtime
Class<?> clazz = Class.forName("Building");
Building building = (Building) clazz.getDeclaredConstructor().newInstance();Explanation:
Class.forName("Building")→ loads the class using its namegetDeclaredConstructor().newInstance()→ creates object- No
new Building()used!
3. Using clone() Method
The clone() method creates a copy of an existing object instead of creating a new one manually.
Steps to Use clone():
- Your class must implement
Cloneable - You must override the
clone()method - Then you can copy the object
class Building implements Cloneable {
int floors;
String color;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Building original = new Building(5, "Blue");
Building cloned = (Building) original.clone();Output:
Original: 5, BlueClone: 5, Blue4. Using Deserialization
Deserialization is a technique of reading an object from the saved state in a file. The object is recreated from a stored byte stream.
ObjectInputStream in = new ObjectInputStream(new FileInputStream("building.ser"));
Building building = (Building) in.readObject();
in.close();Output:
Building with 5 floorsSummary
| Method | When to Use | Key Feature |
|---|---|---|
| new keyword | Standard object creation | Most common way |
| Reflection | Frameworks, dynamic loading | Runtime class loading |
| clone() | Copying existing objects | Creates duplicate |
| Deserialization | Loading saved objects | From byte stream |
Standard object creation
Most common way
Frameworks, dynamic loading
Runtime class loading
Copying existing objects
Creates duplicate
Loading saved objects
From byte stream
