Pritish Chakraborty wrote:
Yes, it will be accessible in the other package, in C. The difference will be that i will act as C's own private member, as C can see it only through inheritance, meaning C's methods can only access it directly. - I may be wrong about this one though. Someone please clarify!
The dot operator cannot be used across differing packages unless the variable is public, which defeats the purpose of a closed form of inheritance
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.
Pritish Chakraborty wrote:But I thought you can't access a member of a class from a different package using dereferencing operator o.O
Please clarify, Henry..this has left me confused.
Pritish Chakraborty wrote:Think of the protected access specifier as a package level + child level access specifier.
Pritish Chakraborty wrote:
1. If outside the package, the access level will only be child level. This means that you cannot access the member using an object and dot operator.
Pritish Chakraborty wrote:
I presume you were going through the section in K&B in which the author has differentiated between [b]default and protected.[/b]
Pritish Chakraborty wrote:I cannot try as I am not on the computer yet, but considering that the inherited 'i' variable acts like it is private to class C as per K&B, it shouldn't be accessible by the dot operator like that ._. this changes everything.
Pritish Chakraborty wrote:I cannot try as I am not on the computer yet, but considering that the inherited 'i' variable acts like it is private to class C as per K&B, it shouldn't be accessible by the dot operator like that ._. this changes everything.
Sanjeev Ba wrote:Once the subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclasses of the subclass.
1. What is its access level for subclasses of subclass
2. Does it have the same access level for subclasses of this subclass inside and outside the subclass's package.
Sanjeev Ba wrote:
1. What is its access level for subclasses of subclass
Sanjeev Ba wrote:Ok, that answers my question. Thanks to both Henry and Pritish for your discussions.
Henry Wong wrote:
The "responsible for the implementation of that object" rule applies, but let's leave that out for this question.
gurpeet singh wrote:Hi Henry, I'm not able to get this at all. now class B can access the protected variable 'i' of class A through inheritance. now in class B the access level of 'i' will be protected(as henry said accessibility cannot change with inheritance). now class C extends class B. let us apply "responsible for the implementation rule" here. now 'i' is the protected member of an object of class B. it can be access from outside the package only by code that is responsible for the implementation of THAT OBJECT i.e. B object. that is it should be IS -A B. but you said that it should be IS -A C.
Sanjeev Ba wrote:Henry,
In the example posted by Henry, class C belongs to a different package (pkg C) than class B.
So, protected member "i" of class B can be accessed in class C only via inheritance and therefore "b.i" will not compile, because it is accessing using instance var.
I think the question is, what is "THAT OBJECT". From the context it looks like "THAT OBJECT" is B.
But, "The code responsible for the implementation of that object" is class C.
But when in class C, we do
C c = new C();
System.out.print(c.i);
we are accessing the inherited member which really means C has a member "i" due to inheritance, which stated otherwise is "Code that is responsible for the implementation of that object.
Pritish Chakraborty wrote:I cannot try as I am not on the computer yet, but considering that the inherited 'i' variable acts like it is private to class C as per K&B, it shouldn't be accessible by the dot operator like that ._. this changes everything.
'Code that is responsible for the implementation of the object' - most vague gem of a JLS line I have heard all day!
So can I say that protected basically means that each class has a copy of the inherited member in its own space, in default-like fashion?
Pritish Chakraborty wrote:
Nevermind, why introduce ambiguity? Let's do away with the 'copy' word altogether.
Basically in an inheritance tree regardless of packages, a protected member at the top of the tree is available in default-like access for the rest of the tree.
This should end it.
gurpeet singh wrote:
you have misinterpreted what is written in the book. as per the book the protected member of a subclass becomes private to any code outside the subclass i.e. even if a class is in the same package as the subclass(the book called it neighbour) then it won't be able to access the protected variable and that it would be private for it. now here is the part which you left - the book also mentions "WITH THE EXCEPTION OF SUBCLASS OF SUBCLASS".
so that means subclass of the subclass will be able to access the protected variable. which also means accessibility does not change with inheritance.
Henry Wong wrote:
gurpeet singh wrote:
you have misinterpreted what is written in the book. as per the book the protected member of a subclass becomes private to any code outside the subclass i.e. even if a class is in the same package as the subclass(the book called it neighbour) then it won't be able to access the protected variable and that it would be private for it. now here is the part which you left - the book also mentions "WITH THE EXCEPTION OF SUBCLASS OF SUBCLASS".
so that means subclass of the subclass will be able to access the protected variable. which also means accessibility does not change with inheritance.
Not sure what you are trying to say here.... but I think you are trying to say.... If class A and B are in the same package, and class A defines a protected variable, and class B subclass from class A, then other classes in the same package as A and B can only access the protected variable via an instance of A (but not of B). This is, of course, not correct. So, can you elaborate what you mean?
Henry
Sanjeev Ba wrote:Once the subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclasses of the subclass.
1. What is its access level for subclasses of subclass
2. Does it have the same access level for subclasses of this subclass inside and outside the subclass's package.
Twitter:ben_pheonix
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |