C

Core Java tutorial for beginners

Clean • Professional

Core 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.

🏆 Top 5 Interview Questions - Classes & Objects

1. What is the difference between a class and an object?

Answer:

  • Class: Blueprint or template defining attributes and behaviors.
  • Object: Instance of a class with its own state and behavior.

Example: Car is a class, myCar is an object of Car.


2. What is a constructor in Java?

Answer:

  • A constructor is a special method used to initialize objects.
  • Name of constructor = class name, no return type.
  • Can be default (no args) or parameterized.

Example:

Car(String color, int speed) { this.color = color; this.speed = speed; }

3. Can a class have multiple objects?

Answer: Yes, a single class can be used to create multiple objects, each with its own state.

Example:

Car car1 = new Car("Red", 60);
Car car2 = new Car("Blue", 90);

4. How can objects store different states?

Answer: Each object has its own set of instance variables, so multiple objects of the same class can have different values.

Example:

Car car1 = new Car("Red", 60);
Car car2 = new Car("Blue", 90);

car1.speed is 60, car2.speed is 90 → different states.


5. How do classes and objects support OOP principles?

Answer:

  • Encapsulation: Use private fields with getters/setters.
  • Abstraction: Hide implementation details.
  • Reusability: One class can produce many objects.
  • Polymorphism: Objects can behave differently via method overloading/overriding.

Article 0 of 0