aspose file tools*
The moose likes Java in General and the fly likes accessing protected members Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "accessing protected members" Watch "accessing protected members" New topic
Author

accessing protected members

Satish Ray
Greenhorn

Joined: Jul 13, 2006
Posts: 16
why cannot we access protected members of SuperClass from the SubClass using the reference of the SuperClass?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

Protected members are accessible in subclasses and in classes in the same package.

Can you explain your question with a short piece of code?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Satish Ray
Greenhorn

Joined: Jul 13, 2006
Posts: 16
Hi Jesper,
Thanks for your reply.

Here is my concern:

package pkgA;
public class SuperClassA {
protected int superClassVarA;
protected void superClassMethodA() {}

}

package pkgB;
import pkgA.*;

public class SubClassB extends SuperClassA {

SubClassB objRefB = new SubClassB(); ----->line1
//SuperClassA objRefA=new SuperClassA();----->line2
void subclassMethodB(SuperClassA objRefA) {
objRefB.superClassMethodA();
objRefB.superClassVarA = 5;
objRefA.superClassMethodA(); //this will not compile

objRefA.superClassVarA = 10; //this will not compile
}
}

The compiler throws an error saying that superClassMethodA() has protected access in pkgA.superClassMethodA();

Even when I create the reference objRefA of SuperClassA in class SubClassB as commented in line2
in the above code the compiler shows the same message.

My concern is why cannot i access the protected member of the SuperClassA in one pakage from the SubClassB in the other package using the reference of the SuperClassA.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Like Jesper said: protected members of a class are accessible by classes in the same package AND by it's subclasses unless the package.

In the SubClass at "line 2" you creates a new instance of SuperClass (so you are not extending/subclassing it!), but it's protected members aren't visible, just because the class is in another package.
[ October 11, 2006: Message edited by: Bauke Scholtz ]
Satish Ray
Greenhorn

Joined: Jul 13, 2006
Posts: 16
I am still not very convinced with the explanation.

i)If a remove the comment in line2 and use the reference of superclassA in the code of subclassB, why cannot I access the protected members of the superclassA in SubClassB?

package pkgA;
public class SuperClassA {
protected int superClassVarA;
protected void superClassMethodA() {}

}

package pkgB;
import pkgA.*;

public class SubClassB extends SuperClassA {

SubClassB objRefB = new SubClassB(); ----->line1
//SuperClassA objRefA=new SuperClassA();----->line2
void subclassMethodB(SuperClassA objRefA) {
objRefB.superClassMethodA();
objRefB.superClassVarA = 5;
objRefA.superClassMethodA(); //this will not compile

objRefA.superClassVarA = 10; //this will not compile
}
}

ii) what if I comment line2 in the above code and I directly use objRefA in the code of SubClassB as shown below:
objRefA.superClassMethodA();
objRefA.superClassVarA = 10;

what happens in this case and why?

Please provide me the expalantion for both the questions (i)& (ii) I have raised in this request.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
1) I already have given the explanation. You're creating a new instance of the superclass outside the package and you're not subclassing it.

2) Because you have outcommented the declaration and instantiation of objRefA, so it's not accessible. Anyway, even if it is instantiated, you still cannot access it's abstract methods, see 1) for the explanation.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I don't think anyone has precisely answered Satish's question. There's a subtle point here that many people don't know about.

If you look in the Java Language Spec, section 6.6.2, you'll see that

]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.


That means, plain and simple, that class X can't access the protected members of an instance of class Y just because X is a subclass of Y. Your expectation about what ought to work is simply not right.

On the other hand, if class Z is in turn a subclass of X, then the protected members declared in Y can be accessed in instances of Z by code in X. Got that?


[Jess in Action][AskingGoodQuestions]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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.
But, this is the same as:
protected members of a class are accessible by classes in the same package AND by it's subclasses unless the package.


That means, plain and simple, that class X can't access the protected members of an instance of class Y just because X is a subclass of Y. Your expectation about what ought to work is simply not right.
Supposing that X is outside of the Y's package: even if X is not a subclass of Y, it still cannot access the protected members of Y. So I don't see something new in here?
On the other hand, if class Z is in turn a subclass of X, then the protected members declared in Y can be accessed in instances of Z by code in X. Got that?
This is called inheritance and it is nothing more than logical.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

For your first point, there's a subtle but important problem with the statement "protected members of a class are accessible by classes in the same package AND by it's subclasses unless the package." To simplify things, assume that each class we mention is in its own package.

Class Y defines a protected member M.
Classes X and Z are subclasses of Y.


The subtle point is that code in class X can access M only on instances of class X or its subclasses. Code in class X can not access M via an instance of class Y itself, or on instances of any other subclasses of Y. So in class X, we would have



It's the third and fourth lines that the original poster is asking about, and that is not captured by the sentence I've quoted above. It's this subtle issue that surprises people.

As for your last point about my last statement being a logical result of inheritance: yes, it is. But given the point discussed above, it's important to remember that sometimes logical deductions about inheritance can fail. For example, given these same classes, it is true that X[] and Z[] will both extend Y[] , but...

Atul Shukla
Ranch Hand

Joined: Oct 09, 2006
Posts: 34
"in other package protected member are , only can inherited to subclass ", in other package you can access protected member of class only by subclassing it , ther is no other way .. that is why superclass refrence variable has no access to protected member because that does not fullfill the condition of accessing through inheritance....

-Atul
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Originally posted by Ernest Friedman-Hill:
It's the third and fourth lines that the original poster is asking about, and that is not captured by the sentence I've quoted above. It's this subtle issue that surprises people.
This is true. I also have stated it in my first post in this topic:
In the SubClass at "line 2" you creates a new instance of SuperClass (so you are not extending/subclassing it!), but it's protected members aren't visible, just because the class is in another package.


And:
As for your last point about my last statement being a logical result of inheritance: yes, it is. But given the point discussed above, it's important to remember that sometimes logical deductions about inheritance can fail. For example, given these same classes, it is true that X[] and Z[] will both extend Y[] , but...

this is interesting stuff to think about
[ October 12, 2006: Message edited by: Bauke Scholtz ]
Satish Ray
Greenhorn

Joined: Jul 13, 2006
Posts: 16
I am grateful to all of you for providing such a nice explanation which i cannot get in any books.
I could able to understand the same now. But still some gaps are there in my understanding the concept. Please provide me the necessary explanation for the queries rasied below.

Thnaks in advance to one and all.

Under section 6.6.2 of the java specifications,

Quote:
-----------------------------------------------------------
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.
----------------------------------------------------------------------

Here what does it mean "the implementation of that object" --in what way?

Also,
under section 6.6.2.1 of the java specifications,

Quote:
--------------------------------------------------------------
6.6.2.1 Access to a protected Member
Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:

If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.

If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.
_________________________________________________________________
what is the difference between ExpressionName (Q) and the Primary expression?


Quote:
_________________________________________________________________________________
Also in example 6.6.7 of the java specifications:
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).
___________________________________________________________________________________
what I didnot understand here is:
a)Point3d is not involved in the implementation of a Point.
b)Point3d, a subclass of Point is involved in the implementation of a Point3d.

Actually what does this imply:-->involving/not involved in the implementation of an object.
I am aware that we can implement the interfaces, where the classes implementing the interfaces have the contract with the interface being implemented by the corresponding classes. But what is this: implementation of an object?
Satish Ray
Greenhorn

Joined: Jul 13, 2006
Posts: 16
Hi Ernest,

Thnaks for the nice pointers you had provided on this topic. Also I am having some concerns after going through the specifications where in which I have raised the doubts in my earlier posted reply. Can you explain me as what is meant by the involvement of the subclass in the implementation of the object of the superclass as is explained in the java specification 6.6.7 example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing protected members