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.


Java
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.


UML class diagram with an Account box above a SavingsAccount box and a hollow-headed arrow pointing up from SavingsAccount to Account.


A test program runs these lines:


Java
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.


600.0+600.0×0.04=600.0+24.0=624.0600.0 + 600.0 \times 0.04 = 600.0 + 24.0 = 624.0


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:


Java
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.



In Java, a class is declared as class Truck extends Vehicle. The class Vehicle has a private field vin, a protected field mileage, and two constructors: Vehicle() and Vehicle(String vin). All classes are in different packages. Which of the following statements are true? Select all that apply.

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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 { }

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Consider these two Java classes, both in the same package.


Java
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:


Java
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.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors