Java Reference
In-Depth Information
3.5. What protected Really Means
We noted briefly that making a class member protected means it can be
accessed by classes that extend that class, but that is loose language.
More precisely, beyond being accessible within the class itself and to code
within the same package (see Chapter 18 ) , a protected member can also
be accessed from a class through object references that are of at least
the same type as the classthat is, references of the class's type or one
its subtypes. An example will make this easier to understand.
Consider a linked-list implementation of a queue, class SingleLinkQueue ,
with methods add and remove for storing an object at the tail of the queue
and removing the object from the head of the queue, respectively. The
nodes of the queue are made up of Cell objects that have a reference
to the next cell in the queue and a reference to the object stored in the
current cell.
class Cell {
private Cell next;
private Object element;
public Cell(Object element) {
this.element = element;
}
public Cell(Object element, Cell next) {
this.element = element;
this.next = next;
}
public Object getElement() {
return element;
}
public void setElement(Object element) {
this.element = element;
}
public Cell getNext() {
return next;
}
public void setNext(Cell next) {
 
Search WWH ::




Custom Search