top of page

Classes and Objects

🎯 Concept:

A class is a blueprint; an object is a real-world entity created from that blueprint.

🏦 Real-World Scenario: Banking System – Modeling a Bank Account

✅ Use Case:

You're building a banking app that tracks user accounts, balances, and basic operations like deposit and withdraw. 🔹 Java Code Example: Class with Fields, Constructor, and Methods

public class BankAccount {

// Fields (state)

private String accountHolder;

private String accountNumber;

private double balance;


// Constructor (how objects are initialized)

public BankAccount(String accountHolder, String accountNumber, double initialDeposit) {

this.accountHolder = accountHolder;

this.accountNumber = accountNumber;

this.balance = initialDeposit;

}


// Method (behavior)

public void deposit(double amount) {

if (amount > 0) {

balance += amount;

System.out.println("Deposited: ₹" + amount);

}

}


public void withdraw(double amount) {

if (amount > 0 && amount <= balance) {

balance -= amount;

System.out.println("Withdrawn: ₹" + amount);

} else {

System.out.println("Insufficient balance or invalid amount.");

}

}


public void displayBalance() {

System.out.println(accountHolder + "'s Balance: ₹" + balance);

}

}


 Usage (Creating Objects)


public class Main {

public static void main(String[] args) {

BankAccount account1 = new BankAccount("Aarav Mehta", "ACC123", 5000);

account1.deposit(2000);

account1.withdraw(1500);

account1.displayBalance();

}

}


💬 How to Explain in Interview

"In this example, I modeled a BankAccount as a class with three core attributes: holder name, account number, and balance. The constructor sets the initial state. The behaviors — deposit, withdraw, and displaying balance — are modeled through methods. This object-oriented structure mirrors real-world banking logic, which makes it intuitive and easy to extend later (e.g., for interest calculation, loan accounts, etc.)."

🧠 Interview Follow-ups You Can Prepare For

Question

How to Respond

What is the difference between a class and an object?

A class is the definition; an object is an instance in memory created from that class.

How is this different from a record class?

record classes in Java 21 are used for immutable data transfer. For behavior-rich objects like bank accounts, regular classes are preferred.

Can we make BankAccount immutable?

Yes, but then we'd have to return new copies for every operation like deposit/withdraw. In this case, mutability models real-world bank accounts better.

🔧 Practice Challenge for Student

Task:Model a MobilePhone class that includes:

  • brand, model, price

  • a method to apply discount

  • a method to print full specs

Recent Posts

See All
Inheritance in Java

🎯 Goal of Inheritance Inheritance enables code reuse  and creates a hierarchical relationship  between classes. It allows a subclass to...

 
 
 
Encapsulation with Getters/Setters

🎯 Goal of Encapsulation Encapsulation is the practice of hiding internal object state  and exposing access via controlled methods...

 
 
 

Comments


bottom of page