• 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

When a class is involved in the implementation of another class?

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
And what "implementation of a class" means here?
Here is the link: http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#36191
and here are the code and the explanation of what's hapening.
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.
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).

The question:
What does it mean that a class is "involved in the implementation" of another class, I don't undesrstand the meaning, how is this working?
My problem is that I cannot a logical approcah to this problem.
Thanks,
Cristian
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that "implementation of a class" here means the ability for a class to determine the initialization code of a field, or to determine the implementation of a method:
A class can always initialize the fields that declares, and implement the method that declares as their will. The same can be said for those of its members inherited by subclasses.
But a class is not responsible for the initialization or implementation of members that are declared in any of its supertypes. The responsible class is the one that declared them.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cristian
Check out this link for a previous discussion on this topic.
There have been many others too. Click on the search link at the top of this page and type in 'protected implementation' (without the quotes) as your search words, you'll get a few othe links that might help you out.

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Nothing up my sleeve ... and ... presto! A tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic