...
/Solution Review: Implement an Account Class using Polymorphism
Solution Review: Implement an Account Class using Polymorphism
This review provides a detailed analysis to solve the 'Implement an Account Class using Polymorphism' challenge.
We'll cover the following...
Solution #
Press + to interact
// Account Classclass Account {protected double balance; // protected variablepublic Account(double balance) { // parametrized constructorthis.balance = balance;}// member functionspublic void Deposit(double amount){}public void Withdraw(double amount){}public void printBalance(){}}// Savings class extended from Account classclass Savings extends Account {double interestRate = 0.8; // member variablepublic Savings(int balance) { // parametrized contructorsuper(balance); // calling base class constructor}// Implementation of member functionspublic void Deposit(double amount) {balance += amount + (amount * interestRate);}public void Withdraw(double amount) {balance -= amount + (amount * interestRate);}public void printBalance() {System.out.println("Balance in your saving account: " + balance);}}// Current class extended from the Account classclass Current extends Account {public Current(int balance) { // Parametrized constructorsuper(balance); // calling base class constructor}// Implementation of public member functionspublic void Deposit(double amount) {balance += amount;}public void Withdraw(double amount) {balance -= amount;}public void printBalance() {System.out.println("Balance in your current account: " + balance);}}class Demo {public static void main(String args[]) {// creating savings account objectAccount SAccount = new Savings(50000);SAccount.Deposit(1000);SAccount.printBalance();SAccount.Withdraw(3000);SAccount.printBalance();System.out.println();// creating current account objectAccount CAccount = new Current(50000);CAccount.Deposit(1000);CAccount.printBalance();CAccount.Withdraw(3000);CAccount.printBalance();}}
Explanation #
-
We have implemented the
Account
class which has the balance ...