Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on protected

 
John Daniel
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Balraj Momi
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for taking the time to answer my little query.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic