This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected members inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Protected members inheritance" Watch "Protected members inheritance" New topic
Author

Protected members inheritance

Stephane Weber
Ranch Hand

Joined: Mar 07, 2002
Posts: 110
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
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
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
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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 ]

SCJP2. Please Indent your code using UBB Code
Stephane Weber
Ranch Hand

Joined: Mar 07, 2002
Posts: 110
Ok, clear now ...
Thanks Jamal and Jose ...

Stephane
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 348
I understand now protected members, but what about protected methoeds, can this be called by an instance in a subclass, in a other package ??
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
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

Joined: Jun 05, 2002
Posts: 106
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].
Amir Ghahraei
Greenhorn

Joined: Jun 20, 2002
Posts: 5
There is a very good explanation on this in Khalid Mugal and Rolf Rasmussen book!
explains it very clearly
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Could someone please explain what 'implementation of an object' means? I have no idea.
Thanks!
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
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 ]

SCJP2
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
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.


Let us be showered in the light of confusion!
shuaib_khan
Greenhorn

Joined: Jun 21, 2002
Posts: 2
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected members inheritance
 
Similar Threads
protected
protected access specifier
I'm gonna SNAP!
proctected -- Pl HELP
Predict the output