• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

I'm gonna SNAP!

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay ... now I am REALLY confused. I thought I understood access modifiers, but I guess I don't. Consider the following code from 6.6.7 of the Java Language Specs (http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#36191)
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.
What the heck is going on? Since when did "implementations" play a role in accessing member fields? I DON'T EVEN KNOW WHAT "not involved in the implementation" MEANS!!!
Could someone please help this lost lamb? WHY do those lines in Delta method fail to compile?
Little-Lost-Dan
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


6.6.2 Details on protected Access
A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.


Which translates to english as: Point3d can ONLY get at the protected stuff of Point that is inherited internally to Point3d through it's sub-class status, it can NOT get at the protected stuff of Point by going directly through Point.
In these lines:
p.x += this.x; // compile-time error: cannot access p.x
p.y += this.y; // compile-time error: cannot access p.y
You are trying to access p.x and p.y through p. Well p is only related to Point, not to Point3D so the sub-class relationship is not in effect. When you use this.x and this.y you are accessing protected fields of Point THROUGH the sub-class thereby establishing the relationship, so this is fine.
You MUST use the sub-class relationship to get at the protected stuff.
[This message has been edited by Cindy Glass (edited July 27, 2001).]
 
Dan Temple
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. Thanks - I feel less punchy now. Now if Point and Point3d were in the same PACKAGE however then delta would compile correctly, right? I wouldn't have to ensure that the object of Point involved Point3d in its implementation. Right?
Dan
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it !
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic