This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Protected Access Modifier...

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
M Sharma
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marlene.
 
M Sharma
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a ton Marlene
Your example and explaination were right on the target ...
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic