Class and Object in Java

Class and Object in Java play fundamental role in defining the structure and behaviour of Java programs.

In Java, a class is a blueprint or a template that defines the structure and behavior of objects. It encapsulates data (attributes) and methods (functions) that operate on that data. A class acts as a user-defined data type, allowing us to create objects based on that blueprint.

An object, on the other hand, is an instance of a class. It represents a real-world entity or concept, and it is created based on the defined class. Each object has its own unique state (values of instance variables) and can perform actions (invoke methods) as defined in the class.

Let’s understand with an example:

public class Car {
    // Fields (instance variables)
    String make;
    String model;
    int year;

    // Constructor
    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    // Method
    public void start() {
        System.out.println("The " + year + " " + make + " " + model + " is starting.");
    }
}

In this example, we have defined a class named Car. It has three fields (make, model, and year), a constructor (a special method used to initialize the object), and a method (start).

To use this class and create objects, we can do the following:

public class Main {
    public static void main(String[] args) {
        // Create two Car objects
        Car myCar = new Car("Toyota", "Corolla", 2022);
        Car yourCar = new Car("Honda", "Civic", 2023);

        // Access the fields and methods of the Car objects
        System.out.println("My car details: " + myCar.make + " " + myCar.model + " " + myCar.year);
        myCar.start();

        System.out.println("Your car details: " + yourCar.make + " " + yourCar.model + " " + yourCar.year);
        yourCar.start();
    }
}

Output:

My car details: Toyota Corolla 2022
The 2022 Toyota Corolla is starting.
Your car details: Honda Civic 2023
The 2023 Honda Civic is starting.

In this example, we created two objects myCar and yourCar based on the Car class. Each object has its own state (values of make, model, and year), and they can perform the action defined by the start method.

In summary, a class is a blueprint or template for creating objects, and an object is an instance of a class with its own unique state and behavior. By defining classes and creating objects, we can organize and encapsulate data and functionality, making our code more modular, reusable, and easier to maintain. Object-oriented programming revolves around the concept of classes and objects, allowing us to model real-world entities and interactions in our Java programs.

FAQs

What is a class in Java?

A class in Java is a blueprint or template for creating objects. It defines the properties (attributes) and behaviors (methods) that objects of that class will have.

What is an object in Java?

An object in Java is an instance of a class. It represents a real-world entity and can be used to access the properties and invoke the methods defined in the class.

How do I create an object in Java?

To create an object in Java, you use the new keyword followed by the class constructor. For example: ClassName objectName = new ClassName();

What is the difference between a class and an object?

A class is a blueprint that defines the structure and behavior of objects, while an object is an instance of a class. In other words, a class is a template, and objects are created based on that template.

Can a Java class have multiple objects?

Yes, a Java class can have multiple objects created from it. Each object is independent and has its own set of properties and can invoke methods defined in the class. Multiple objects of the same class can coexist in a Java program.