• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected access specifier

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following is a quote from Khalid’s book :
“A subclass in another package can only access protected members in the
superclass via reference of its own type or a subtype.”
I have not understood this. The only thing I know about the protected access
specifier is that, members marked as protected are accessible even outside
the package, but only to subclasses, provided the class is accessible. Can anybody please explain this with the help of an example.
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The following will help.


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.

 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The method delta could try to cast (�5.5, �15.16) 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 compile-time 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).

 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a good example -- protected is the least understood modifier. Probably because books often say, "Protected means subclasses outside the package can access the protected member."
Sort of... the access is restricted JUST TO INHERITANCE. So a subclass can access the member by inheriting it, and that's it. The subclass doesn't get to make an instance of the parent class and then access the member that way, using the dot operator.
It's like the parent class says, "I'm marking this member protected, so that you -- my subclass outside the package -- can use it, but the only way you get to use it because you inherit it. So you can read it and write it, but you can't give anyone else access to it! It behaves as though it were private to your class when YOU use it. It's as if you inherited a private member, that only you get to use. Nobody who has a reference to you can use your reference to see it (well, unless they are another instance of your same class, but that's always true for private members). And you cannot use a reference to me -- the superclass -- and access it using the dot operator on a reference to me. If you try to do that, it will be as if it were private to ME. But again, you DO get to inherit it, so that you can use it in your own methods, etc."
cheers,
Kathy
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic