Example: Tracing a SavingsAccount subclass
A bank's software stores every account as an Account object. A savings account needs everything an ordinary account has, plus an interest rate. Instead of copying the Account code, the programmer writes SavingsAccount as a subclass. With inheritance, a subclass receives the fields and methods of its superclass and adds members of its own.
public class Account {
private double balance;
protected int transactions;
public Account(double opening) {
balance = opening;
transactions = 0;
System.out.println("Account opened");
}
public void deposit(double amount) {
balance += amount;
transactions++;
}
public double getBalance() {
return balance;
}
public int getTransactions() {
return transactions;
}
}
public class SavingsAccount extends Account {
private double rate;
public SavingsAccount(double opening, double rate) {
super(opening);
this.rate = rate;
System.out.println("Savings ready");
}
public void addInterest() {
deposit(getBalance() * rate);
}
}The UML class diagram below shows the same two classes. A minus sign marks a private member, a hash marks a protected member and a plus sign marks a public member.
A test program runs these lines:
SavingsAccount s = new SavingsAccount(500.0, 0.04);
s.deposit(100.0);
s.addInterest();
System.out.println(s.getBalance());
System.out.println(s.getTransactions());a) What does the first line of the test program print, and in what order?
The expression new SavingsAccount(500.0, 0.04) calls the SavingsAccount constructor. Its first statement is ________, which hands control to the constructor of the ________ class before any other SavingsAccount constructor code runs. In Java, a constructor can call super(...) at most once, and it cannot use the object before that call. In the Java versions most courses use (before Java 25), the call must also be the first statement of the constructor.
The Account constructor sets balance to 500.0 and transactions to 0, then prints Account opened. Control returns to the SavingsAccount constructor, which stores the rate 0.04 in rate and prints Savings ready.
The output is Account opened on the first line and Savings ready on the second.
This order is constructor chaining: the superclass constructor finishes before the rest of the subclass constructor body runs, so subclass code after the super(...) call can rely on inherited fields already holding valid values.
b) What do the last two lines of the test program print?
SavingsAccount declares no deposit method, so s.deposit(100.0) runs the method inherited from Account. The balance becomes ________ and the transaction count becomes ________.
Next, s.addInterest() computes the interest from the inherited getter and the subclass's own rate, 600.0 × 0.04 = ________, then passes that amount to the inherited deposit method. That deposit raises the balance and adds one more transaction.
The program prints 624.0 on one line and 2 on the next: one transaction from the direct deposit and one from the deposit inside addInterest.
c) A teammate proposes this replacement for addInterest:
public void addInterest() {
balance = balance + balance * rate;
transactions++;
}Does it compile? If not, which line fails, and how should the method be written?
The field balance is declared ________ in Account. Every SavingsAccount object still contains a balance, but a private member can be named only inside the class that declares it. The first line therefore fails to compile, and the compiler reports that balance has private access in Account.
The second line compiles on its own, because transactions is protected and a subclass may use protected members of its superclass directly.
The fix is to reach the balance through Account's public methods, which is what the original version does: deposit(getBalance() * rate);. Since deposit already increments transactions, the separate transactions++ line should be removed; keeping it would count the interest payment twice.
A video library models every Film as a MediaItem. A Library stores media items but is not itself a media item. Assume MediaItem is an existing class that permits subclasses.
Complete the Java declaration for Film using the appropriate keyword and superclass name:
class Film { }
Consider these two Java classes, both in the same package.
class Counter {
protected int count;
Counter(int start) {
count = start;
}
void step() {
count += 2;
}
int getCount() {
return count;
}
}
class BoostCounter extends Counter {
private int boost;
BoostCounter(int start, int boost) {
super(start * 2);
this.boost = boost;
}
void boostedStep() {
step();
count += boost;
}
}A program runs the following lines:
BoostCounter b = new BoostCounter(3, 5);
b.step();
b.boostedStep();
b.boostedStep();
System.out.println(b.getCount());What number does the program print? Give your answer as an integer.