Card 07/ 08
All 8 cards
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.
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?
| Member | Inside Invoice | Ledger, same package | Receipt, subclass elsewhere | Report, unrelated |
|---|---|---|---|---|
private long pence | Yes | No | No | No |
long auditId | Yes | Yes | No | No |
protected String owner | Yes | Yes | Yes, on itself | No |
public String reference | Yes | Yes | Yes | Yes |
private void recalc() | Yes | No | No | No |
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.