jQuery in Action, 2nd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "protected" Watch "protected" New topic
Author

protected

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 3901
6.6.7 Example: protected Fields, Methods, and Constructors
Consider this example, where the points 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 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.
My question is if you add a method to class Point: protected abstract void delta(Point p)
and make Point an abstract class then class Point3d will compile?


I never took notes in college. That's how I got a 4.0 the first 2 years, and a 3.5 the second two years.
Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
What's wrong?
I tried to add protected abstract void delta(Point p)
to class Point,but class Piont3d stiil can not compile.
Why?
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Bin,
The trick with protected is that it can only be used by code that is involved in the implementation of the object.
In the example, a Point object is passed to the method delta(). As far as the compiler is concerned, delta() is not code in the same package and the fact that delta() is declared in the body of a subclass is irrelevant.
The alternate delta(Point3D p) works because the creation of Point3D actually involved the creation of a Point. A Point3D object is a subclass of Point and subclass's inherit protected members. ie you can use a subclass reference to access protected members BUT methods cannot access protected members simply because they are defined in a subclass.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiousity.
-- Dorothy Parker


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
 
subject: protected
 
Threads others viewed
When a class is involved in the implementation of another class?
protected access between instances
Protected members inheritance
protected acces modifier
protected access specifier
MyEclipse, The Clear Choice