Create a parent class “Account” with three variables num, title and bal with two functions withdraw and deposit. Create a child class “SavingAccount” with a function calculateProfit (that applies 7.5% profit on current balance). Finally create the class “TestApp” that creates the object of Saving account and call withdraw, deposit functions and public data members of Account class. Override withdraw function in SavingAccount to deduct Rs. 250 if the balance after withdrawal of amount is less than 10000.
public class Main
{
public static void main(String[] args) {
SavingAccount s=new SavingAccount();
s.num=1234;
s.title="ABC";
s.bal=0.0;
s.deposit(5000);
System.out.println("Profit = "+s.calculateProfit());
s.withdraw(250);
System.out.println("Profit = "+s.calculateProfit());
}
}
class Account{
public int num;
public String title;
public double bal;
public void withdraw(int amount){
bal=bal-amount;
}
public void deposit(int amount){
bal=bal+amount;
}
}
class SavingAccount extends Account{
public double calculateProfit(){
return (0.075*bal);
}
public void withdraw(int amount){
if((bal-amount)<10000)
bal=bal-amount;
System.out.println("You have withdrawn "+amount+". Current balance is "+bal);
}
}
Comments
Leave a comment