protected

Method syntax

>>-protected-+----------+-+--------+-+-------+-+--------------+->
             '-abstract-' '-static-' '-final-' '-synchronized-'

>-+--------+-ResultType-Identifier-(-+---------------------+-)->
  '-native-'                         '-FormalParameterList-'

>-+----------------------+-MethodBody-><
  '-throws-ClassTypeList-'

Field syntax

>>-protected-+----------+-+--------+-+-----------+-Type->
             +-final----+ '-static-' '-transient-'
             '-volatile-'

>-+-Identifier--------------+-+------------------------+-;-><
  |                 v-----' | '-=-+-Expression-------+-'
  '-ArrayIdentifier-+-[-]-+-'     '-ArrayInitializer-'

Description
A protected class member or constructor can only be accessed from the class where it is declared, subclasses of that class, and other classes within the package containing that class.

Example
Consider this example, where the package called points is declared:

package points;

public class Point {
    protected int x, y;

    void warp(threePoint.Point3d a) {
        if (a.z > 0)             // compile-time error: cannot access a.z
            a.delta(this);
        }
}

Then consider the threePoint package, which includes a definition of  class Point3d:

package threePoint;
import points.Point;

public class Point3d extends Point {
    protected int z;
    
    public void delta(Point p) {
        p.x += this.x;             // compile-time error: cannot access p.x
        p.y += this.y;             // compile-time error: cannot access p.y
    }

    public void delta3d(Point3d q) {
        q.x += this.x;
        q.y += this.y;
        q.z += this.z;
    }
}

A compilation error occurs in the method delta: it cannot access the protected members x and y of its parameter p, because while Point3d (the class in which the references to fields x and y occur) is a subclass of Point (the class in which x and y are declared), it is not involved in the implementation of a Point (the type of the parameter p). The method delta3d can access the protected members of its parameter q, because the class Point3d is a subclass of Point and is involved in the implementation of a Point3d.

The method delta could try to cast its parameter to be a Point3d, but this cast would fail, causing an exception, if the class of p at run time were not Point3d.

A compilation error also occurs in the method warp: it cannot access the protected member z of its parameter a, because while the class Point (the class in which the reference to field z occurs) is involved in the implementation of a Point3d (the type of the parameter a), it is not a subclass of Point3d (the class in which z is declared).

In the following example, the public and protected fields x, y, useCount and totalUseCount are inherited in all subclasses of Point:

package points;

public class Point {
   public int x, y;
   protected int useCount = 0;
   static protected int totalUseCount = 0;

   public void move(int dx, int dy) {
       x += dx; y += dy; useCount++; totalUseCount++;
   }
}

Therefore, this test program, in another package, can be compiled successfully:

class Test extends points.Point {
    public void moveBack(int dx, int dy) {
        x -= dx; y -= dy; useCount++; totalUseCount++;
    }
}

ngrelr.gif (548 bytes)
Syntax diagrams
Access control
abstract keyword
class keyword
final keyword
interface keyword
private keyword
public keyword
static keyword
synchronized keyword
transient keyword

Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.