Attributes & Methods

Lesson 0004 — 20 min read

Attributes = state (what an object knows). Methods = behavior (what an object does). Together they define an object's interface.

Attributes

Attributes (fields/properties) hold the state of an object. They should typically be private to enforce encapsulation — external code shouldn't directly touch internal data.

// Java
private String name;
private double balance;

Methods

Methods define what an object can do. They operate on attributes and provide controlled access to them.

Getters & Setters

BankAccount Class

class BankAccount {
    private String name;
    private double balance;

    public BankAccount(String name, double balance) {
        this.name = name;
        this.balance = balance;
    }

    public void setName(String name) { this.name = name; }
    public String getName() { return name; }
    public double getBalance() { return balance; }

    public void deposit(double amount) {
        balance += amount;
    }

    public boolean withdrawal(double amount) {
        if (amount > balance) {
            System.out.println("Insufficient amount");
            return false;
        }
        balance -= amount;
        return true;
    }
}
class BankAccount {
private:
    string name;
    double balance;

public:
    BankAccount(string name, double balance) {
        this->name = name;
        this->balance = balance;
    }

    void setName(string name) { this->name = name; }
    string getName() { return name; }
    double getBalance() { return balance; }

    void deposit(double amount) {
        balance += amount;
    }

    bool withdrawal(double amount) {
        if (amount > balance) {
            cout << "Insufficient amount" << endl;
            return false;
        }
        balance -= amount;
        return true;
    }
};
class BankAccount:
    def __init__(self, name, balance):
        self.__name = name
        self.__balance = balance

    def setName(self, name):
        self.__name = name

    def getName(self):
        return self.__name

    def getBalance(self):
        return self.__balance

    def deposit(self, amount):
        self.__balance += amount

    def withdrawal(self, amount):
        if amount > self.__balance:
            print("Insufficient amount")
            return False
        self.__balance -= amount
        return True

Understanding the Interaction

Attributes are private — external code cannot touch them directly. Methods bridge the gap:

Important Points

Code trace: What does this print?

BankAccount acc = new BankAccount("Raj", 1000);
acc.deposit(500);
acc.withdraw(200);
System.out.println(acc.getBalance());

What happens if you call acc.withdrawal(9999) on an account with balance 100?

Notes

Attributes :-

Attribute ⇒ data/state of object (eg name, balance)

Make private ⇒ prevents direct external modification

Methods :-

Method ⇒ behavior/action of object

a) Getter ⇒ retrieves private attribute (eg getBalance())

b) Setter ⇒ modifies private attribute (eg setName())

c) Action ⇒ operates on state with validation (eg deposit(), withdrawal())

BankAccount Example :-

Goal ⇒ model real account with controlled access

Private attributes ⇒ name, balance

Public methods ⇒ setName, getName, getBalance, deposit, withdrawal

Key interaction:

a) Private balance ⇒ external code cannot mutate directly

b) Deposit / withdrawal ⇒ validate rules before mutating

c) GetBalance ⇒ provides safe read-only access

Important Points :-

a) Encapsulation ⇒ private fields + public methods ; protects invariants

b) Validation ⇒ check conditions before state mutation (eg insufficient balance)

c) Default values ⇒ Java: 0/null/false ; C++: garbage ; Python: set in __init__

Primary source: Oracle Java Tutorials — OOP Concepts. Ask me anything that's unclear.

← Prev: Class & Object Next: Constructors →

Questions? Ask your agent — you can follow up on any concept, quiz answer, or how this applies to a specific coding problem. Review the Java glossary or syntax quick reference for a compressed overview.