Java Inheritance Programming - BankAccount class with methods called deposit() and withdraw()
Write a Java program to create a class known as "BankAccount" with methods called deposit() and withdraw(). Create a subclass called SavingsAccount that overrides the withdraw() method to prevent withdrawals if the account balance falls below one hundred.
Sample Solution:
Java Code:
Output:
Create a Bank Account object (A/c No. BA1234) with initial balance of $500: Deposit $1000 into account BA1234: New balance after depositing $1000: $1500.0 Withdraw $600 from account BA1234: New balance after withdrawing $600: $900.0 Create a SavingsAccount object (A/c No. SA1234) with initial balance of $450: Balance after trying to withdraw $300: $150.0 Create a SavingsAccount object (A/c No. SA1000) with initial balance of $300: Try to withdraw $250 from SA1000! Minimum balance of $100 required! Balance after trying to withdraw $250: $300.0
Explanation:
The BankAccount class has a constructor that takes account number and balance as arguments. It also has methods to deposit and withdraw money, and to check the account balance.
The SavingsAccount class is a subclass of BankAccount and overrides the withdraw() method. It checks if the account balance falls below one hundred before allowing a withdrawal. The method prints an error message if the balance is below one hundred. If the balance is greater than or equal to one hundred, the method calls the withdraw() method of the superclass to withdraw.
In Main() method -
The main method begins by creating an instance of the BankAccount class with an account number of "BA1234" and an initial balance of $500. It then deposits $1000 into the account and displays the new balance. It then withdraws $600 from the account and displays the new balance.
Next, the method creates an instance of the SavingsAccount class with an account number of "SA1234" and an initial balance of $450. It then attempts to withdraw $300 from the account and displays the new balance. Since the balance remains above the minimum $150 balance required for the account, the withdrawal is successful.
Finally, the method creates another instance of the SavingsAccount class with an account number of "SA1000" and an initial balance of $300. It then attempts to withdraw $250 from the account, which would bring the balance below the minimum balance required for the account. The method displays the new balance after the attempted withdrawal, which should still be $300 since the withdrawal was unsuccessful.
Flowchart:



For more Practice: Solve these Related Problems:
- Write a Java program where the "SavingsAccount" subclass automatically transfers excess funds to a fixed deposit.
- Write a Java program where the "BankAccount" class prevents duplicate account numbers.
- Write a Java program where the "SavingsAccount" subclass adds a method to calculate yearly interest.
- Write a Java program where the "BankAccount" class includes fraud detection for unusual transactions.
Go to:
Java Code Editor:
Contribute your code and comments through Disqus.
PREV : Employee class with methods called work, getSalary.
NEXT : Animal Class with a method move() .
What is the difficulty level of this exercise?