6.6.7
Example:
protected
Fields, Methods, and Constructors
NAMES
the
public
class
Point
has as
public
members the
move
method and the
moves
field. These
public
members are accessible to any other package that has access
to package
points
. The fields
x
and
y
are not
public
and therefore are accessible
only from within the package
points
.
6.6.7 Example:
protected
Fields, Methods, and Constructors
Consider this example, where the
point
package declares:
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);
}
}
and the
threePoint
package declares:
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;
}
}
which defines a class
Point3d
. A compile time error occurs in the method
delta
here: 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 sub
class 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
.
104
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved