Skip to content

Commit fdd5bf9

Browse files
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

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

Section12Inheritance/src/InheritanceIMP.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//Solve it pending
21
class Account {
32
private String accNo;
43
private String name;
@@ -79,7 +78,6 @@ public void repay(long amt) {
7978
}
8079
}
8180

82-
// Testing the classes in main method
8381
public class InheritanceIMP {
8482
public static void main(String[] args) {
8583
System.out.println("=== Savings Account Transactions ===");
@@ -94,4 +92,4 @@ public static void main(String[] args) {
9492
la.repay(7000);
9593
System.out.println("Remaining Loan Balance: " + la.getBalance());
9694
}
97-
}
95+
}

0 commit comments

Comments
 (0)