Introduction
Java is an object-oriented programming language. In Java, Class and Object are two important concepts used to organize code and develop applications.
Before understanding Class and Object, let's take a real-world example.
Imagine a car manufacturing company. Before manufacturing a car, the company needs a design or blueprint. The blueprint describes how the car should look and what features it should have. Using that blueprint, the company can manufacture many cars.
In Java, a class works like a blueprint, and an object represents an actual instance created using that blueprint.
What is a Class in Java?
A class is a blueprint or template used to create objects. It defines the properties and behaviours that objects can have.
For example, a car has some properties, such as brand, colour, and speed. It also has behaviours, such as starting, stopping, and moving.
In Java, we can define these properties and behaviours inside a class.
Example:
class Car { // Properties String brand; String color; int speed; // Behaviours void start() { System.out.println("Car is starting"); } void stop() { System.out.println("Car is stopping"); } } In this example:
-
Caris the class name. -
brand,color, andspeedare variables that represent the car's properties. -
start()andstop()are methods that represent the car's behaviours.
A class only defines the structure. It does not automatically create an actual car object.
What is an Object in Java?
An object is an instance of a class. It represents a particular entity with its own identity, state, and behaviour.
For example, consider two cars. One car is a BMW with a black colour, and another is an Audi with a white colour.
Both cars can be represented using the same Car class, but their details are different.
In Java, objects are commonly created using the new keyword.
Example:
Car c1 = new Car(); Here:
-
Caris the class name. -
c1is a reference variable. -
newis the keyword used to create an object. -
Car()calls the constructor.
The reference variable c1 refers to the newly created Car object.
How to Create and Use Objects in Java?
After creating a class, we can create an object and access its variables and methods using the dot (.) operator.
Let's understand this with a Student example.
Example:
class Student { String name; int age; void study() { System.out.println(name + " is studying"); } } public class Main { public static void main(String[] args) { Student s1 = new Student(); s1.name = "Abishek"; s1.age = 21; System.out.println("Name: " + s1.name); System.out.println("Age: " + s1.age); s1.study(); } } Output:
Name: Abishek Age: 21 Abishek is studying In this program, a Student class is created with two variables, name and age, and a method called study().
First, an object called s1 is created using the new keyword. Then, values are assigned to its variables.
Finally, the study() method is called using the object reference.
The dot operator is used to access the object's variables and methods.
Creating Multiple Objects from One Class
One of the useful features of classes is that we can create multiple objects using a single class.
For example, a college management application may contain hundreds of students. Instead of creating a separate class for every student, we can create one Student class and use it to create multiple student objects.
Example:
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(); s1.name = "Abishek"; s1.age = 21; s2.name = "Arun"; s2.age = 22; s1.display(); s2.display(); } } Output:
Abishek - 21 Arun - 22 Here, both objects are created using the same Student class. However, each object maintains its own values for name and age.
This makes it easier to manage multiple students using a single class.
Difference Between Class and Object
Class and Object are related concepts, but they have different purposes.
| Class | Object |
|---|---|
| A class is a blueprint or template. | An object is an instance of a class. |
| It defines properties and behaviours. | It has its own data and behaviour. |
It is declared using the class keyword. | It is commonly created using the new keyword. |
| One class can create multiple objects. | Each object represents a particular instance. |
| Example: Student | Example: s1, s2 |
For example, Student is a class, while individual students can be represented as different objects of that class.
Why Do We Use Classes and Objects?
Classes and objects help developers organize programs and represent real-world entities.
Some important advantages are:
1. Code Reusability
A single class can be used to create multiple objects without writing the same structure repeatedly.
2. Code Organization
Related variables and methods can be grouped together inside a class.
3. Easy Maintenance
Organized code is easier to understand, modify, and maintain.
4. Real-World Representation
Real-world entities such as students, employees, cars, products, and bank accounts can be represented using classes and objects.
5. Object-Oriented Programming
Classes and objects provide the foundation for important OOP concepts such as inheritance, encapsulation, polymorphism, and abstraction.
Conclusion
Class and Object are the basic building blocks of object-oriented programming in Java. A class defines the structure and behaviours, while an object represents an instance created using that class.
Classes help organize related data and methods, while objects allow us to work with individual entities and their data.
Understanding these concepts is important for developing Java applications and learning advanced object-oriented programming concepts.

