Answer: It will not compile Comments: Although, class B extends class A and 'i' is a protected member of A, B still cannot access i, (now this is imp) THROUGH A's reference because B is not involved in the implementation of A. Had the process() method been defined as process(B b); b.i would have been accessible as B is involved in the implementation of B. For more information read Section 6.6.7 of JLS: ______________________________________________________________ My Problem:- What does it mean by "B is not involved in the implementation of A". I have read the section 6.6.7 of JLS but could not get it. If anybody can explain me the meaning it would be of great help Thanks [ August 17, 2003: Message edited by: Thomas Paul ]
Munish Gulati<br />SCJP 1.4<br />Albert Einstein: There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.
Howdy, I'm not sure what they were trying to explain, but I can tell you what the problem is... (and this is perhaps the most commonly misunderstood part of Java access, so pretty much everybody has trouble with this!) It centers around what the 'protected' modifier really means. You'll often see it in charts and tables like this: default: accessible only to classes in the same package. protected: accessible only to classes in the same package or subclasses, even when the subclasses are outside the package. But that just doesn't explain it!! Because for 'protected', you really do NOT have full access to a protected member of your superclass, if you're a subclass and outside the package of the superclass. 'protected' means "Access through Inheritance ONLY". In other words, if a member is marked 'protected', subclasses outside the package can access that member ONLY by inheriting it. The subclass "has" the member, but they can't ACCESS the member using an instance of the superclass.
So, class B can't make an instance of A and then access the protected 'i' variable. But... class B can access this.i. B can get his *own* inherited 'i' variable, but he cannot ask for the 'i' variable of an instance of A, the superclass. Bottom line: You can't use a superclass reference to get to a protected member of your superclass if you are not in the same package as the superclass. However, you CAN use your own 'this' reference to access the protected member, because you definitely inherited it. One more note while we're here... With protected, what happens to the subclass outside the package, when IT has subclasses? Can those subclasses still inherit the protected 'i'? Yes. Once a member is marked 'protected', that member is inherited all the way down regardless of where the other subclasses live (in other words, it makes no difference which packages the subclasses are in). BUT... once you have a subclass outside the package, the *other* classes in the subclass-outside-the-package's package cannot access the protected member. For example, ANY class in A's package can access 'i' -- it acts default within the class in which the member was declared. But within B, nobody in B's package can access 'i', so from that perspective the protected member acts as though it were marked private! Except... it isn't *completely* private, because subclasses of B will still *inherit* the member. So, 'protected' is a very strange modifier, because it means "access through inheritance only", except for other classes defined within the same package as the class that declared the protected member (in which case, the protected member is just like 'default' access). Whew! I probably just made things more confusing cheers, Kathy
An object of class B cannot access protected methods inherited from class A invoked on objects through references of type A or C. 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. [ August 18, 2003: Message edited by: Marlene Miller ]
There is no "i" in denial. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss