Difference Between Object And Class (original) (raw)

Last Updated : 21 Jan, 2026

In Java, classes and objects are core concepts of Object-Oriented Programming (OOP). Understanding the difference between them is essential for writing structured and reusable code.

For Example, Dog is a class, Tommy is an object of that class.

Class

What is a Class?

A Class is a blueprint or template used to create objects. It defines data members (fields) and member functions (methods) that describe the behavior of an object. When a class is defined, no memory is allocated.

**Example: An account is a class that defines common properties, such as name, I, and balance.

What is an Object?

An Object is an instance of a class. It represents a real-world entity and allows access to the class members. Memory is allocated only when an object is instantiated.

**Example: SBI and ICICI are objects created from the Account class.

class

**Example:

Java `

class Account{

private String name;
private int id;
private double balance;
private double money;

public Account() {
    
}

public Account(String name, int id, double balance)
{
    this.name = name;
    this.id = id;
    this.balance = balance;
}
public String getName(){
    return name; 
    
}
public void setName(String name){
    this.name = name; 
    
}

public int getId(){
    return id; 
    
}
public void setId(int id){
    this.id = id; 
    
}

public double getBalance(){
    return balance; 
    
}
public void setBalance(double balance)
{
    this.balance = balance;
}

public double getMoney(){
    return money; 
    
}
public void setMoney(double money)
{
    this.money = money;
}

@Override public String toString()
{
    return "Account [name=" + name + ", id=" + id
        + ", balance=" + balance + "]";
}
public void balanceInquery()
{
    System.out.println(
        name + " Current Balance Is :: " + balance);
}
public String withdrawMoney()
{
    return name + " Withdraw Money Successfully";
}

} public class GFG{

public static void main(String[] args)
{

    Account SBI = new Account("Raghab", 2211, 70000.00);
    Account ICICI = new Account("Navi", 1001, 90000.00);

    System.out.println(SBI);
    SBI.balanceInquery();
    SBI.setMoney(5000);
    System.out.println(SBI.withdrawMoney());

    System.out.println(
        "--------------------------------");

    System.out.println(ICICI);
    ICICI.balanceInquery();
    ICICI.setMoney(1000);
    System.out.println(ICICI.withdrawMoney());
}

}

`

Output

Account [name=Raghab, id=2211, balance=70000.0] Raghab Current Balance Is :: 70000.0 Raghab Withdraw Money Successfully

Account [name=Navi, id=1001, balance=90000.0] N...

**Explanation: This program demonstrates how a class defines the structure and behavior of an account, while objects represent real accounts created at runtime. The Account class encapsulates account details and operations, and the main method creates account objects to check balance and perform withdrawal actions.

class_account

Class vs Object

Basis Class Object
Definition A blueprint or template used to create objects A real instance of a class
Creation Created using the class keyword Created using the new keyword
Memory Allocation Does not occupy memory Occupies memory
Existence Logical entity Physical (runtime) entity
Data Storage Does not store actual values Stores actual data
Purpose Defines structure and behavior Performs operations using class members
Example class Account { } Account acc = new Account();

**Related Topics: