Loose data in main()
In procedural programming, data lives in loose variables such as holder1 and balance1. Functions and data are separate: nothing groups which variables belong to each account.
In procedural programming, data lives in loose variables such as holder1 and balance1.
Functions and data are separate: nothing groups which variables belong to each account.
In procedural programming, data lives in loose variables such as holder1 and balance1. Functions and data are separate: nothing groups which variables belong to each account.
Any line mutates balance1 without control: assigning -500 leaves the account in an invalid state.
That coupling makes the program fragile: a mistake in one place breaks the data for everyone.
Any line mutates balance1 without control: assigning -500 leaves the account in an invalid state. That coupling makes the program fragile: a mistake in one place breaks the data for everyone.
Pillar 1, abstraction: we pick the essentials of the domain and gather them in BankAccount.
An object combines state (balance) and behavior (deposit) under a single responsibility.
Pillar 1, abstraction: we pick the essentials of the domain and gather them in BankAccount. An object combines state (balance) and behavior (deposit) under a single responsibility.
Pillar 2, encapsulation: the field private double balance stays hidden inside the object.
No one can touch it from outside; only the public methods of the object itself access it.
Pillar 2, encapsulation: the field private double balance stays hidden inside the object. No one can touch it from outside; only the public methods of the object itself access it.
When calling deposit(-500), the condition amount > 0 rejects the negative value at the gate.
Instead, deposit(300) passes the rule and updates the protected balance to 1300.0.
When calling deposit(-500), the condition amount > 0 rejects the negative value at the gate. Instead, deposit(300) passes the rule and updates the protected balance to 1300.0.
Pillar 3, inheritance: SavingsAccount extends BankAccount reuses the base state and methods.
The subclass specializes the model without duplicating code: a SavingsAccount is a BankAccount.
Pillar 3, inheritance: SavingsAccount extends BankAccount reuses the base state and methods. The subclass specializes the model without duplicating code: a SavingsAccount is a BankAccount.
Pillar 4, polymorphism: a BankAccount variable sends the same message calculateInterest().
At runtime, each object responds according to its real type: SavingsAccount computes its 10%.
Pillar 4, polymorphism: a BankAccount variable sends the same message calculateInterest(). At runtime, each object responds according to its real type: SavingsAccount computes its 10%.
From loose variables to cohesive objects: protected state and behavior in a single unit.
The four pillars —abstraction, encapsulation, inheritance and polymorphism— draw clear boundaries.
That is how modular software is built: each object guards its data and answers well-defined messages.
From loose variables to cohesive objects: protected state and behavior in a single unit. The four pillars —abstraction, encapsulation, inheritance and polymorphism— draw clear boundaries. That is how modular software is built: each object guards its data and answers well-defined messages.