← Chapter 6 — Object-Oriented Programming

22. Classes & Objects

mixed

Description

A class is a blueprint that describes data and behavior. An object is a concrete instance created from that blueprint.

For example, a Student class can describe a name and score, while Aditi and Rohan are individual Student objects with their own values.

Subtopics

Fields

Description

Fields store an object's state.

Explanation

Each object gets its own field values.

Example

String name;
int score;

Creating objects

Description

new creates an object from a class.

Explanation

The variable stores a reference to that object.

Example

Student aditi = new Student();

Explanation

Putting related data and behavior together makes programs easier to understand. Start with small classes whose responsibility is obvious.

Example

public class StudentDemo {
    static class Student {
        String name;
        int score;
    }

    public static void main(String[] args) {
        Student aditi = new Student();
        aditi.name = "Aditi";
        aditi.score = 92;
        System.out.println(aditi.name + ": " + aditi.score);
    }
}

Key points

  • A class is a blueprint.
  • An object is a class instance.
  • Fields hold object state.

Video

Open video

Open reference