• 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 members inheritance

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am stumbling on this example ... This looks odd to me. Can someone please explain to me ?
In the following extract :
package point;
class Point {
protected int x, y;
}
package threepoint;
import point.Point;
class ThreePoints extends Point {
protected int z;

public void delta(Point p) {
p.x += this.x; // compile-error: cannot access p.x
p.y += this.y; // compile-error: cannot access p.y
}
}

Even though ThreePoints is a subclass of Point, it cannot access the protected fields in Point.
But all the texts I read state that however these are not accessible, they are however inherited.
How should I understand inherited then ?

Thanks,

Stephane
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Stephane
JLS 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
JLS 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 Poin (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).

That's all.
Jamal Hasanov
ww.j-think.com
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It follows that a class is involved in the implementation of an object, if the type of the object is the class or one of its subclasses. That is, a Point3d instance can access protected members declared in Point3d or subclasses, but not in Point.
protected access yields access in the same package. But outside the package in which the protected members are declared, the access is denied. It is possible to access the protected members declared in the class (or its subclasses) where the access expression ocurrs. That class (outside the package in which some class declares protected members, but a subclass of it) is not responsible for the mentioned protected members, it is only responsible for the protected members it declares (or its subclasses do)
[ May 31, 2002: Message edited by: Jose Botella ]
 
Stephane Weber
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, clear now ...
Thanks Jamal and Jose ...

Stephane
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand now protected members, but what about protected methoeds, can this be called by an instance in a subclass, in a other package ??
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I am totally confused!
Of course a subclass is not responsible for the
implementation of the super class!
So no code in a subclass is ever responsible for the implementation of a super class.
Then " A protected field is open to subclasses
declared in different pakages." is just junk and
should be deleted from JLS.
Maybe everybody is showing how we cannot access
the protected members in the super class in a
different pakage so that I do not understand
the idea.
Can anyone give an example that shows how:
A subclass declared in different pakage from its
super class can access its super class protected
members please?
Thanks a lot. I am very trouble with this issue.
 
Alan Chong
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I forget to tell you that if you use the code labels to enclose your code, your code will retain its original margins.
End you code with [/CODE] and start your code
with [CODE].
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a very good explanation on this in Khalid Mugal and Rolf Rasmussen book!
explains it very clearly
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone please explain what 'implementation of an object' means? I have no idea.
Thanks!
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope the following two examples will make this clear.
True or False: A subclass declared in a different package from its super class can access protected members of its super class.
Answer: True.
Example:


True or False: A subclass declared in a different package from its super class can access protected members of an object whose type is its super class.
Answer: False.
Example:

[ June 25, 2002: Message edited by: Tybon Wu ]
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had questions like this elsewhere. I think I understand this, but let me express it and tell me if I really do understand it or I am just too confused to know the reality
A superclass extended by a subclass will have all its code accessable to the subclass. Now, if the superclass is in different package than the subclass, a subclass will only be able to access a protected field/method if the subclass create an object of itself (since this subclass object would inherat all the stuff in the superclass). If the subclass create an object that is the superclass type, then that object is treated like a normal object created from types that this subclass has not extend, therefor its protected field/method can't be accessed.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friend
derived class in any package can only access the protected members of a class using objects of it's own types, or using objects of it's subtypes.
protected members cannot be accessed from the object of the class whose members are being accessed
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic