Commit fdd5bf9
committed
feat: Add Account hierarchy with SavingsAccount and LoanAccount for deposit and loan operations
WHAT the code does:
Defines Account as a base class with private fields:
- accNo, name, address, phno, dob, and protected balance.
Provides getters for all fields and setters for address and phone.
Defines SavingsAccount extending Account:
- deposit(long amt): increases balance with validation.
- withdraw(long amt): decreases balance if sufficient funds exist.
Defines LoanAccount extending Account:
- Constructor initializes balance with loanAmount.
- payEMI(long amt): reduces outstanding loan balance.
- repay(long amt): pays back loan, ensures balance does not go negative.
Defines InheritanceIMP with main():
- Demonstrates savings account deposits and withdrawals.
- Demonstrates loan account EMI payments and full repayment.
WHY this matters:
Demonstrates inheritance to model different account types with shared attributes.
Encapsulates common data in a superclass while extending functionality in subclasses.
Shows polymorphism in action: both subclasses rely on the same balance field but apply different business rules.
Provides a realistic banking-style example with deposits, withdrawals, EMIs, and repayments.
HOW it works:
SavingsAccount sa starts with balance = 0, deposit(5000) → balance = 5000, withdraw(2000) → balance = 3000.
LoanAccount la starts with balance = 10000, payEMI(3000) → balance = 7000, repay(7000) → balance = 0.
Program prints all transactions and final balances.
Tips and gotchas:
balance is protected—subclasses can access directly, but encapsulation would be stronger if balance were private with protected methods for updates.
Error handling is minimal; in real applications, invalid transactions would throw exceptions instead of printing messages.
Account currently allows setting address/phone but not other fields; immutability for accNo and name is appropriate since they rarely change.
Naming convention: InheritanceIMP could be renamed BankDemo or AccountTest for clarity.
Use-cases:
Educational demonstration of inheritance and specialization.
Simple simulation of banking systems for deposits, withdrawals, and loans.
Foundation for extending into polymorphic designs where Account references handle different account types uniformly.
Short key: class-account savings-loan inheritance banking-demo.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 6d7d86b commit fdd5bf9
1 file changed
+1
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | 2 | | |
4 | 3 | | |
| |||
79 | 78 | | |
80 | 79 | | |
81 | 80 | | |
82 | | - | |
83 | 81 | | |
84 | 82 | | |
85 | 83 | | |
| |||
94 | 92 | | |
95 | 93 | | |
96 | 94 | | |
97 | | - | |
| 95 | + | |
0 commit comments