wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected Access Modifier... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Protected Access Modifier..." Watch "Protected Access Modifier..." New topic
Author

Protected Access Modifier...

M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
I am referring Khalid Mughal for my SCJP preparation. On page no. 117 (chapter 4. Declaration and Access control) i came across the explaination of protected Members where it says....
A subclass in another package can only access protected members in the superclass via references of its own type or a subtype.
There is a example given for protected modifier...
package A;
public class SuperclassA
{
protected int superclassVarA;
protected void superclassMethodA()
{/*..........*/}
}
package B;
public class SubclassB extends SuperclassA
{
SuperclassA objRefA = new SubclassB();
SubclassB objRefB = new SubclassB();
void subclassMethodB()
{
objRefA.superclassMethodA(); //OK
objRefB.superclassVarA; //NOT OK
//My doubt is WHY objRefA.superclassVarA is NOT OK
}
}
Further explaination is given that, "Access to protected members of the superclass would also be permitted via any references of subclasses of SubclassB. The above restriction helps to ensure that subclasses in packages different from their superclass can only access protected members of the superclass in their part of inheritance hierarchy."

Why i can't access the protected member of superclass by using reference variable of type superclass, since it belongs to the same inheritance hierarchy
Can someone help me out on this topic....
TIA
Manish Sachdev
[ August 30, 2003: Message edited by: Manish Sachdev ]
[ September 01, 2003: Message edited by: Manish Sachdev ]

Regards, Manish
SCJP 1.4
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
There are some errors on the code.
Once corrected:
objRefA.superclassMethodA();
It is not ok because the compile type of objRefA is "Superclass"
int o = objRefB.superclassVarA;
It is ok because the compile type of objRefB is "Subclass", that is, the same as the one holding the access expression (int o = objRefB.superclassVarA or one of its subclasses.
A class attempting access to a protected member declared in another package is allowed only to access the inherited members, those that are part of an instance of itself, or its subclasses. However such class is not allowed to access the protected members of an instance of the class in another package.


SCJP2. Please Indent your code using UBB Code
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
I have mis-typed the subclassMethodB() method, the correct one is as below:
void subclassMethodB()
{
objRefB.superclassMethodA(); //OK
System.out.println(objRefA.superclassVarA); //NOT OK
//My doubt is WHY objRefA.superclassVarA is NOT OK
}
The book gives the following explaination :-
The class SubclassB defines two instance variables, one of own type (objRefB) and one of its superclass's type (objRefA). As can be seen access is permitted to a protected member of the SuperclassA in packageA by a reference of the subclass, but not by a reference of its superclass. Access to protected members of the superlcass would also be permitted via any references of subclasses of SubclassB. Access to protected members of the superclass would also be permitted via any references of subclasses of SubclassB. The above restriction helps to ensure that subclasses in packages different from their superclass can only access protected members of the superclass in their part of inheritance hierarchy
Pls explain....!!!
when i compile the code the error which it gives me is :
superclassVarA has protected access in packageA.superClass
System.out.println(objRefA.superclassVarA);
1 error
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Manish, it is common for people to be confused by protected access. It takes extra effort and thought. Here is the visual idea in my mind.

B objects should not be allowed to access methods in C objects which both B and C have inherited from A.
1. An object of class B cannot access protected methods inherited from class A invoked on objects through references of type A or C.
2. An object of class B can only access protected methods inherited from class A invoked on objects through references of type B and subclasses of B.
[ September 01, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is an explanation which Kathy Sierra recently gave to Karan Here
I learned about protected access from The Java Programming Language, 3.5 What protected Really Means. Here is the rationale they give.
The reasoning behind the restriction is this: Each subclass inherits the contract of the superclass and expands that contract in some way. Suppose that one subclass, as part of its expanded contract, places constraints on the values of protected members of the superclass. If a different subclass could access the protected members of objects of the first subclass then it could manipulate them in a way that would break the first subclass's contract -- and this should not be permissible.

[ September 01, 2003: Message edited by: Marlene Miller ]
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
Thank you Marlene for your efforts to explain me the concept...
"Each subclass inherits the contract of the superclass and expands that contract in some way. Suppose that one subclass, as part of its expanded contract, places constraints on the values of protected members of the superclass"
"If a different subclass could access the protected members of objects of the first subclass then it could manipulate them in a way that would break the first subclass's contract -- and this should not be permissible."
Can you please elaborate on the sentence marked bold in the above para with an example...like how a subclass will place constraints on value of protected members of the superclass and how would it break the contract.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is an example where subclass C places constraints on the value of the protected field x. x must be >= 0. Subclass B violates that constraint.

Suppose subclass B has access to the protected field x of A by using a reference to an A object.

We set x in the B object to �10 and x in the C object to +20. We swap the values. Now x in C is �10. This violates the constraint of C.
[ September 02, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Manish, I have a more realistic, convincing example with queues that I will show you next. But I don't want to overload you. One example at a time.
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
I understand that subclasses in OTHER packages can access protected members of the superclass through inheritance ONLY
I thought that classes in the SAME package as the superclass could access the member normally...but the following code threw me off.
package p1;
public class SuperclassA
{
protected int superclassVarA;
protected void superclassMethodA()
{/*..........*/}
}

package p1;
class OtherClass
{
SuperclassA aa = new SuperclassA();
int ac = aa.superclassVarA; //this compiles
aa.superclassVarA = 10; // this does not..what am I missing here?
}
Please explain.
Thanks,
Cathy.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Cathy,
This statement needs to be inside a method or a constructor.
aa.superclassVarA = 10; // this does not..what am I missing here?
These two statements are okay, because they are field declarations.
SuperclassA aa = new SuperclassA();
int ac = aa.superclassVarA; //this compiles
You are correct, classes can access protected members of other classes in the sames package.
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Thanks Marlene.
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
Thanx a ton Marlene
Your example and explaination were right on the target ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected Access Modifier...