

extends
public 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!

display method of the ChoiceQuestion class
public void display()
{
// Display the question text
super.display(); // OK
// Display the answer choices
. . .
}
public class Question
{
. . .
public void display() { . . . }
}
public class ChoiceQuestion extends Question
{
. . .
public void display() { . . . }
}
Question q = ... q.display();
public class ChoiceQuestion extends Question
{
void display(String title) { ... }
...
}
does not override
public class Question
{
void display() { ... }
...
}Suppose we define
public class CheckingAccount extends BankAccount
{
public void withdraw(double amount, String purpose)
{
super.withdraw(amount);
System.out.printf("Withdrawing $%.2f for %s\n", amount, purpose);
}
}
What does this code snippet print?
BankAccount b = new CheckingAccount(); b.deposit(1000); b.withdraw(100, "cell phone");
Withdrawing $100.00 for cell phoneBalance: $900.00superpublic 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)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 method
Why does the deposit method of the CheckingAccount class call super.deposit?
super in order to avoid recursionsuper as the first statementpublic void transfer(double amount, BankAccount other)
{
withdraw(amount);
other.deposit(amount);
}
withdraw gets called? this.withdraw(amount)thisBankAccount myAccount = new CheckingAccount(); myAccount.transfer(1000, anotherAccount);
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?
Rectangle box = new Rectangle(5, 10, 20, 30); String s = box.toString(); // Sets s to "java.awt.Rectangle[x=5,y=10,width=20,height=30]"
"box=" + box; // Result: "box=java.awt.Rectangle[x=5,y=10,width=20,height=30]"
BankAccount momsSavings = new BankAccount(5000); String s = momsSavings.toString(); // Sets s to something like "BankAccount@d24606bf"
public String toString()
{
return "BankAccount[balance=" + balance + "]";
}
public class BankAccount
{
public String toString()
{
return getClass().getName() + "[balance=" + balance + "]";
}
}
public class SavingsAccount
{
public String toString()
{
return super.toString() + "[interestRate=" + interestRate + "]";
}
}
BankAccount acct = new SavingsAccount(0.5); System.out.println(acct);
What does it print?
BankAccount[balance=0.0] SavingsAccount[balance=0.0] SavingsAccount[balance=0.0, interestRate=0.5] SavingsAccount[balance=0.0][interestRate=0.5]if (coin1.equals(coin2)) . . . // Contents are the same
if (coin1 == coin2) . . . // Objects are the same
ArrayList.contains calls equals
public class Coin
{
. . .
public boolean equals(Object otherObject)
{
Coin other = (Coin) otherObject;
return name.equals(other.name) && value == other.value;
}
}
public class CollectibleCoin extends Coin
{
. . .
public boolean equals(Object otherObject)
{
CollectibleCoin other = (CollectibleCoin) otherObject;
return getName().equals(other.getName()) && getValue == other.getValue()
&& year == other.getYear;
}
}
Coin c1 = new Coin("quarter", 0.25);
Coin c2 = new CollectibleCoin("quarter", 0.25, 1993);
What do c1.equals(c2) and c2.equals(c1) return?
truefalsetrue, the second one returns falsetrue, the second one throws an exceptionpublic class Coin
{
public boolean equals(Object otherObject)
{
if (getClass() != otherObject.getClass()) return false;
...
}
}
super.equals
public class CollectibleCoin extends Coin
{
public boolean equals(Object otherObject)
{
if (!super.equals(otherObject)) return false;
...
}
}
