Is there a way to access a private field in a parent class from a subclass?
ie:
public class parent<T> {
private int MaxSize;
...
}
public class sub extends parent {
public int getMaxSize() {
return MaxSize;
}
}
Basically i want an accessor method, getMaxSize(), to return the maximum size of an ArrayQueue. Thanks.
No –
privatefields can only be directly accessed within the class in which they are declared. You could make the fieldprotected, however, which would allow you to access it from subclasses. The table below is a handy reference:[source]
Of course you can also write a
public(orprotected!) getter method which would just return the value of your field, and use this method in the subclass instead of the actual field itself.Just as an aside, it is convention to write variable names in camelCase in Java, i.e.
maxSize.