C

Core Java tutorial for beginners

Clean • Professional

Java – Classes & Objects in Java with Examples

3 minute

Classes & Objects in Java

In Java, classes and objects are the building blocks of Object-Oriented Programming (OOP). Understanding them is essential for writing organized, modular, and reusable code.


What is a Class?

A class is a blueprint or template for creating objects.

It defines:

  • Attributes (fields) – the data or state of an object.
  • Methods – the behavior or actions of an object.

Syntax:

class Car {
    String color;
    int speed;

    void drive() {
        System.out.println("Car is driving at " + speed + " km/h");
    }
}

Notes:

  • Car → class name
  • color and speed → attributes
  • drive() → method defining behavior

What is an Object?

An object is an instance of a class.

Each object has its own state and can perform behavior defined by its class.

Creating Objects:

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();   // Creating an object
        myCar.color = "Red";     // Assigning value to attribute
        myCar.speed = 80;
        myCar.drive();           // Calling method
    }
}

Output:

Car is driving at 80 km/h

Notes:

  • myCar → object of class Car
  • Multiple objects can be created from the same class

Key Features of Classes and Objects

learn code with durgesh images

FeatureDescription
EncapsulationHide internal details using private fields and provide getters/setters
ReusabilityUse the same class to create multiple objects
AbstractionDefine essential properties without exposing implementation details
PolymorphismObjects can behave differently depending on context (method overloading/overriding)

Example – Multiple Objects

public class Main {
    public static void main(String[] args) {
        Car car1 = new Car();
        car1.color = "Red";
        car1.speed = 60;

        Car car2 = new Car();
        car2.color = "Blue";
        car2.speed = 90;

        car1.drive();
        car2.drive();
    }
}

Output:

Car is driving at 60 km/h
Car is driving at 90 km/h

Note: Each object has its own state even though it’s from the same class.


Constructor in Classes

A constructor is a special method used to initialize objects when they are created.

Example:

class Car {
    String color;
    int speed;

    // Constructor
    Car(String c, int s) {
        color = c;
        speed = s;
    }

    void drive() {
        System.out.println(color + " car is driving at " + speed + " km/h");
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Green", 100);
        myCar.drive();
    }
}

Output:

Green car is driving at 100 km/h

Note: Constructors make object creation cleaner and more efficient.


Points to Remember

  • A class is a blueprint; an object is an instance.
  • Objects store state (attributes) and behavior (methods).
  • Multiple objects can be created from the same class.
  • Use constructors for initialization.
  • Classes and objects are the core of Java OOP.

Article 0 of 0