Industry Ready Java Spring Boot, React & Gen AI — Live Course
JavaClasses and objects

Class and Object

Understanding the Real World

Everything around us can be seen as an object—a car, a mobile phone, a book, or even a person.
Every object has two important characteristics:

  1. What it knows → its properties (also called attributes or fields)
  2. What it does → its behaviour (also called actions or methods)

This same idea is used in Java to design software using objects.

Real-Life Example

Consider a Human as an object:

  • Properties: name, age, height, weight
  • Behaviours: walking, eating, sleeping, talking

In Java, an object works the same way—data (properties) + actions (methods).

Object-Oriented Programming (OOP)

Java is an Object-Oriented Programming language.
OOP helps us represent real-world entities in code using classes and objects.

Key idea behind OOP:

Real-world problems are solved by modeling real-world objects in the program.

Objects interact with each other to perform tasks, just like real entities in life.

What is a Class?

A class is a blueprint, design, or template for creating objects.

A class defines:

  • What an object contains (properties)
  • What an object can do (methods)

Think of a class as an architect’s plan of a house.
The plan is not a house, but you can create many houses using that plan.
Similarly, from one class, you can create many objects.

class and object

Example

class Human {
    // Properties
    String name;
    int age;

    // Behaviours
    void walk() {
        System.out.println(name + " is walking.");
    }

    void eat() {
        System.out.println(name + " is eating.");
    }
}

Here, Human is the blueprint; it describes what every human object will have and do.

What is an Object?

An object is a real instance created from a class. It occupies memory and contains actual values for its properties.

Example

public class Demo {
    public static void main(String[] args) {
        Human h1 = new Human();   // Creating object
        h1.name = "Navin";        // Assigning values
        h1.age = 30;

        h1.walk();
        h1.eat();
    }
}

Output

Navin is walking.
Navin is eating.

Here, h1 is an object created from the class Human. It has real values for name and age, and it performs actions defined in the class.

In Short

ConceptMeaningReal-World Comparison
ClassA design or blueprintArchitect’s plan of a house
ObjectInstance created from the classActual house built from the plan

Summary

  • Java uses objects to model real-world entities.
  • Objects have properties (data) and behaviours (methods).
  • A class defines what an object will contain and how it will behave.
  • A class itself is not an object; it is only a blueprint.
  • Objects are the actual entities that exist in memory and interact with each other during program execution.

Written By: Shiva Srivastava

How is this guide?

Last updated on