Classes and Objects in Java


In Java, a class is a blueprint for creating objects. Objects are instances of classes, and they represent real-world entities. A class defines properties (fields) and behaviors (methods) that an object can have. In this tutorial, we'll explore how to define a class and create objects from that class.

1. What is a Class?

A class is a user-defined blueprint or prototype from which objects are created. It is a collection of variables (fields) and methods (functions) that define the behaviors and characteristics of objects of that class.

Syntax for declaring a class:

    class ClassName {
        // fields (variables)
        // methods (functions)
    }
        

2. What is an Object?

An object is an instance of a class. When a class is defined, no memory is allocated until an object of that class is created. Each object has its own set of properties and methods defined by the class.

Syntax for creating an object:

    ClassName objectName = new ClassName();
        

3. Example: Defining a Simple Class and Creating an Object

Let's define a simple class called Car that has fields (attributes) and methods (behaviors). We will then create an object of the class and access its attributes and methods.

Step 1: Define the Class

The Car class will have the following fields:

  • brand: The brand of the car.
  • model: The model of the car.
  • year: The manufacturing year of the car.

We will also define a method to display the car's details.

    class Car {
        // Fields
        String brand;
        String model;
        int year;
    
        // Method to display car details
        void displayDetails() {
            System.out.println("Car Brand: " + brand);
            System.out.println("Car Model: " + model);
            System.out.println("Car Year: " + year);
        }
    }
        

Step 2: Create Objects of the Class

Now, let's create an object of the Car class and assign values to its fields. We will also call the displayDetails method to display the car details.

    public class Main {
        public static void main(String[] args) {
            // Create an object of the Car class
            Car myCar = new Car();
    
            // Set the values of the object fields
            myCar.brand = "Toyota";
            myCar.model = "Corolla";
            myCar.year = 2020;
    
            // Call the method to display car details
            myCar.displayDetails();
        }
    }
        

Output:

    Car Brand: Toyota
    Car Model: Corolla
    Car Year: 2020
        

In this example:

  • We created an object myCar from the Car class.
  • We set the values of the fields brand, model, and year.
  • We called the displayDetails method to print the details of the car.

4. Constructors in Java

Constructors are special methods that are called when an object is created. They are used to initialize the fields of an object when it is created. If no constructor is defined, a default constructor is used.

Syntax of Constructor:

    ClassName() {
        // Initialize fields
    }
        

Example: Using a Constructor to Initialize Fields

We can modify the Car class to include a constructor that initializes the fields when an object is created:

    class Car {
        // Fields
        String brand;
        String model;
        int year;
    
        // Constructor to initialize fields
        Car(String brand, String model, int year) {
            this.brand = brand;
            this.model = model;
            this.year = year;
        }
    
        // Method to display car details
        void displayDetails() {
            System.out.println("Car Brand: " + brand);
            System.out.println("Car Model: " + model);
            System.out.println("Car Year: " + year);
        }
    }
        

Now, we can pass the values when creating the object:

    public class Main {
        public static void main(String[] args) {
            // Create an object of the Car class using the constructor
            Car myCar = new Car("Honda", "Civic", 2021);
    
            // Call the method to display car details
            myCar.displayDetails();
        }
    }
        

Output:

    Car Brand: Honda
    Car Model: Civic
    Car Year: 2021
        

5. Access Modifiers

Access modifiers in Java control the visibility of class members (fields and methods). The most common access modifiers are:

  • public: The field or method is accessible from anywhere.
  • private: The field or method is accessible only within the class.
  • protected: The field or method is accessible within the same package or subclasses.
  • default: The field or method is accessible only within the same package (no modifier).

6. Conclusion

In Java, classes and objects are fundamental concepts. A class defines the structure of objects, while objects are instances of the class. You can define properties (fields) and behaviors (methods) inside a class, create objects from it, and initialize them using constructors. By understanding classes and objects, you can model real-world entities and design robust Java applications.





Advertisement