This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on protected 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 "Question on protected" Watch "Question on protected" New topic
Author

Question on protected

John Daniel
Greenhorn

Joined: Mar 27, 2005
Posts: 20
After reading K&B's excellent book twice, I thought I had a firm understanding of protected access. Now that I'm in Mughal's book, however, I realize how wrong I was. I've pasted part of Mughal's question 4.18 below. What I don't understand is why does (3) compile and (7) doesn't. I ran the compile myself to see if Professor Mughal is correct, and of course, he is. (7) really doesn't compile. I just don't understand why. I appreciate any insights anyone can provide.

Question 4.18:
// Filename: A.java
package packageA;

public class A {
protected int pf;
}
--------------------------------------------
// Filename: B.java
package packageB;
import packageA.A;

public class B extends A {
void action(A obj1, B obj2, C obj3) {
pf = 10; // (1)
// obj1.pf = 10; // (2)
obj2.pf = 10; // (3)
obj3.pf = 10; // (4)
}
}

class C extends B {
void action(A obj1, B obj2, C obj3) {
pf = 10; // (5)
// obj1.pf = 10; // (6)
obj2.pf = 10; // (7) **Compiler Error**
obj3.pf = 10; // (8)
}
}

Thank you,
John Daniel
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Well i guess its given in the K&B book that the proctected variable when inherited by a subclass becomes private to other subclasses of that subclass outside the package. because of that pf is not visible to the class c. I guess the compiler error also says that.

Cheers


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

First of all please Use Code Tags when you post a source code.

As Nitish said, a protected member of a super class from a different package is private to that class, that means that only the class itself has access to that protected member. Forget that C is a sub-class of B (it doesn't matter in this case). C cannot access any protected member of a super class of B. For example Object class has a protected finalize method. No other class except class B itself can access that protected member. Any super class however has access to that member. For example class A can access the finalize method an an instance of class B because finalize method is accessible to A. In fact the B class got the finalize method through inheritance from A. This is why line 4 compiles fine...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45

Forget that C is a sub-class of B (it doesn't matter in this case). C cannot access any protected member of a super class of B


I kind of disagree with this. Because C can see protected members of B that were inturn inherited from A. Though It can not access those members using Reference of SuperClass, but It can access it successfully using Its own reference. The reason line 3 compiled because B is Subclass of A and can access protected members of A using its own reference. And now the reason line 7 failed because, It is trying to access super class protected member using superclass reference. And this was the sole reason line 8 compiled fine.


Regards
Balraj Kumar
SCJP 5 95%
SCWCD 82%
Preparing for SCBCD
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Balraj Momi wrote:
Forget that C is a sub-class of B (it doesn't matter in this case). C cannot access any protected member of a super class of B


I kind of disagree with this. Because C can see protected members of B that were inturn inherited from A. Though It can not access those members using Reference of SuperClass, but It can access it successfully using Its own reference. The reason line 3 compiled because B is Subclass of A and can access protected members of A using its own reference. And now the reason line 7 failed because, It is trying to access super class protected member using superclass reference. And this was the sole reason line 8 compiled fine.


I should have said C cannot access any protected member of a super class of B on an instance of B
John Daniel
Greenhorn

Joined: Mar 27, 2005
Posts: 20
Thank you all for taking the time to answer my little query.
 
 
subject: Question on protected