


extends
Which of the following statements is true?
BankAccount is a subclass of
SavingsAccountSavingsAccount method overrides the
deposit method of BankAccountSavingsAccount method inherits the
addInterest method of BankAccountSavingsAccount method inherits the
getBalance method of CheckingAccountpublic class SavingsAccount extends BankAccount
{
public void addInterest()
{
double interest = getBalance() * interestRate / 100;
balance = balance + interest; // Doesn't work
}
. . .
}
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
}
}
Now the addInterest method compiles, but it doesn't update the correct balance!

public class BankAccount
{
. . .
public void withdraw(double amount) { . . . }
}
public class CheckingAccount extends BankAccount
{
. . .
public void withdraw(double amount) { . . . }
}
BankAccount myAccount = ... myAccount.withdraw(1000);
public void transfer(double amount, BankAccount other)
{
withdraw(amount);
other.deposit(amount);
}
withdraw gets called? this.withdraw(amount)thisBankAccount myAccount = ... myAccount.transfer(1000, anotherAccount);
superpublic class CheckingAccount extends BankAccount
{
public void deposit(double amount)
{
transactionCount++;
// Now add amount to balance
deposit(amount); // Not complete
}
. . .
}
this.deposit(amount)super.deposit(amount)class A {
void print(String s) { System.out.print(s); }
void f() { print("f"); g(); }
void g() { print("g"); }
}
class B extends A {
void f() { print("f"); super.f(); }
void g() { h(); }
void h() { print("h"); }
}
A a = new B();
a.f(); // What does it print?
superpublic class CheckingAccount extends BankAccount
{
public CheckingAccount(double initialBalance)
{
// Construct superclass
super(initialBalance);
// Initialize transaction count
transactionCount = 0;
}
. . .
}
super(...) calls superclass constructor,
super.method(...) calls superclass methodWhy does the deposit method of the CheckingAccount class call super.deposit?
super in order
to avoid recursionsuper as the first statement