Why am I able to directly access p
's private number
attribute within Striker
's implementation of the hasSameNumber
method? Seems as though using the getter should be the only (if not, best-practice) way to access it. What am I missing?
public class Football {
public static abstract class FootballPlayer {
private final int number;
FootballPlayer(int num) {
number = num;
}
int getNumber() {
return number;
}
abstract boolean hasSameNumber(FootballPlayer p );
}
public static class Striker extends FootballPlayer {
Striker(int num) {
super(num);
}
boolean hasSameNumber(FootballPlayer p ) {
return this.getNumber() == p.number;
}
}
}
Edit:
The accepted answer shows why this is permissible - thanks! What would be the preferred/best-practice way to access number
though - by getter or direct?