Should we place the addInterest method in the BankAccount class?
SavingsAccount
subclass has an addInterest
method.public class SavingsAccount extends BankAccount
{
added instance variables
new methods
}
SavingsAccount collegeFund = new SavingsAccount(10); // Savings account with 10% interest collegeFund.deposit(500); // OK to use BankAccount method with SavingsAccount object
public class SavingsAccount extends BankAccount { private double interestRate; public SavingsAccount(double rate) { Constructor implementation } public void addInterest() { Method implementation } }
public class SavingsAccount extends BankAccount { private double interestRate; public SavingsAccount(double rate) { interestRate = rate; } public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } }
Which instance variables does an object of class SavingsAccount have?
interestRate
interestRate
and balance
interestRate
, balance
, and
interest
Which methods that you can apply to SavingsAccount objects?
deposit
and withdraw
, but not
addInterest
deposit
and withdraw
, but not
getBalance
addInterest
but not deposit
,
withdraw
, or getBalance
public class SavingsAccount extends BankAccount { public void addInterest() { double interest = getBalance() * interestRate / 100; balance = balance + interest; // Error } . . . }
solvethis problem by adding another instance variable with same name:
public class SavingsAccount extends BankAccount { private double balance; // Don’t . . . public void addInterest() { double interest = getBalance() * interestRate / 100; balance = balance + interest; // Compiles but doesn’t update the correct balance } }
public class BankAccount { . . . public void deposit(double amount) { . . . } public void withdraw(double amount) { . . . } public double getBalance() { . . . } } public class CheckingAccount extends BankAccount { . . . public void deposit(double amount) { . . . } public void withdraw(double amount) { . . . } public void deductFees() { . . . } }
public class CheckingAccount extends BankAccount { . . . public void deposit(double amount) { transactionCount++; // Now add amount to balance balance = balance + amount; // Error } }
public class CheckingAccount extends BankAccount { public void deposit(double amount) { transactionCount++; // Now add amount to balance deposit(amount); // Not complete } . . . }
deposit(amount);
as
this.deposit(amount);
which calls the method we are currently writing ⇒ infinite recursion
public class CheckingAccount extends BankAccount
{
public void deposit(double amount)
{
transactionCount++;
// Now add amount to balance
super.deposit(amount);
}
. . .
}
public class CheckingAccount extends BankAccount { private static final int FREE_TRANSACTIONS = 3; private static final double TRANSACTION_FEE = 2.0; private int transactionCount; . . . public void withdraw(double amount) { transactionCount++; // Now subtract amount from balance super.withdraw(amount); } public void deductFees() { if (transactionCount > FREE_TRANSACTIONS) { double fees = TRANSACTION_FEE * (transactionCount - FREE_TRANSACTIONS); super.withdraw(fees); } transactionCount = 0; } . . . }