Class & Object
Class and Object are the foundation of Object-Oriented Programming (OOP) in Java. Every Java program is built using classes and objects, making this a must-master topic for interviews and real-world development.
What Is a Class?
A class is a blueprint or template used to create objects. It defines:
- Properties (variables / fields)
- Behaviors (methods)
class Car {
String color;
int speed;
void drive() {
System.out.println("Car is driving");
}
}
๐ A class does not occupy memory until an object is created.
What Is an Object?
An object is a real instance of a class.
- Represents real-world entities
- Occupies memory
- Can access class members
Car c = new Car();
Here:
- Car โ class
- c โ reference variable
- new Car() โ object creation
Real-World Analogy
| Concept | Real World Example |
|---|---|
| Class | Blueprint of a house |
| Object | Actual house |
| Variables | Rooms, color |
| Methods | Open door, lock door |
Structure of a Class in Java
class ClassName {
// variables
dataType variableName;
// methods
returnType methodName() {
// logic
}
}
Example: Class & Object Program
class Student {
String name;
int age;
void display() {
System.out.println(name + " " + age);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "John";
s1.age = 20;
s1.display();
}
}
Object Creation Process (Behind the Scenes)
Student s1 = new Student();
Steps:
- JVM loads Student class
- Memory allocated in heap
- Instance variables initialized with default values
- Constructor is called
- Reference s1 points to the object
Class Variables vs Instance Variables
class Test {
static int x = 10; // class variable
int y = 20; // instance variable
}
| Type | Memory | Shared |
|---|---|---|
| Class variable | Method area | Yes |
| Instance variable | Heap | No |
Multiple Objects of a Class
Student s1 = new Student();
Student s2 = new Student();
- Each object has its own copy of instance variables
- Methods are shared
Accessing Class Members
Using Object
s1.display();
Using Class Name (static members)
Test.x;
Object Reference Behavior
Student s1 = new Student();
Student s2 = s1;
- Both references point to the same object
- Changes via one reference affect the other
new Keyword Explained
- Allocates memory
- Creates object
- Calls constructor
- Returns object reference
Anonymous Object
Object without reference variable.
new Student().display();
โ Used for one-time use
Why Java Is Object-Oriented (Key Reason)
- Code reusability
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Common Beginner Mistakes
- Confusing class with object
- Accessing non-static members using class name
- Forgetting new keyword
- Assuming class occupies memory
- Creating unnecessary objects
Interview-Ready Answers
Short Answer
A class is a blueprint that defines properties and behaviors, while an object is an instance of a class.
Detailed Answer
In Java, a class defines the structure and behavior of objects using variables and methods. An object is a runtime instance of a class that occupies memory and can access class members. Multiple objects can be created from a single class.
Key Takeaway
Class defines โwhatโ an object is.
Object represents โhowโ it exists in memory.
Mastering class and object concepts is mandatory before learning constructors, inheritance, and polymorphism.