Card 07/ 08
Practice: Which Callers Can Reach Each Member
Five members, four callers in two packages: which combinations compile.
private is this class, no keyword is this package, protected adds subclasses elsewhere, and public is everyone. Using those levels, decide which of the four callers below can reach each of the class's five members, before opening the answer.
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.
← → move through sections and cards