Card 07/ 08

ExerciseDifficulty: Advanced1 min

Five Members, Four Callers: Who Can Reach What

One class with five members, and four different places trying to reach them. Write down which combinations compile before opening the reveal.

java
package billing;

public class Invoice {
    private long pence;
    long auditId;
    protected String owner;
    public String reference;
    private void recalc() { }
}

The four callers: another method inside Invoice; a class Ledger in package billing; a class Receipt in package reporting that extends Invoice; and a class Report in package reporting that does not.

Which of the four can reach each member?
Each member against each caller
MemberInside InvoiceLedger, same packageReceipt, subclass elsewhereReport, unrelated
private long penceYesNoNoNo
long auditIdYesYesNoNo
protected String ownerYesYesYes, on itselfNo
public String referenceYesYesYesYes
private void recalc()YesNoNoNo

Two rows are worth pausing on. auditId has no keyword and is reachable by Ledger, which is wider than leaving the word out usually feels. And Receipt can read owner on itself and not on some other Invoice it happens to be holding — protected across packages reaches your own inherited copy only.