aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected Modifier still a challenge 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 "Protected Modifier still a challenge" Watch "Protected Modifier still a challenge" New topic
Author

Protected Modifier still a challenge

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Refernce : Khalid Mughal

Consider the following code and comments .






Problem : Line 1 is compiling fine, ie accessing subclass inherited protected member through reference variable.

Line 2: Cannot do the same from subclass.

Cannot getting the logic ??? Please explain ???


SCJP 6.0 96%

(Connecting the Dots ....)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

When trying to access a protected member of an instance, that is declared in the super class, and through a reference variable, and the subclass types are in different packages (wow, did I qualify it enough?), then this rule from the JLS applies....

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.


The key phrase is responsible for the implementation. This means that B can only access via B type references. And C can only access via C type references.... and since a C instance IS-A B instance, this means that the B class can access via both B and C type references, but the C class can only access via C type references. And neither B or C class can access using a A type reference. Is that confusing enough? ...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Thanks a lot Henry

Actually i experimented and came to the same conclusion as you gave ( this does not mean i am not appreciating you for your efforts, I always appreciate and infact salute you )

But the thing i am searching for is WHY ? this rule is imposed by Java designers !!!

Nevertheless, for SCJP, let me write what i concluded , you please crosscheck

Referring to above question.

In B class we can refer pf ofcourse by B ref.
In C class we can refer pf ofcourse by C ref.


In B class we can refer pf by C ref. ( Because B is responsible for the implementation )
In C class we CANNOT refer pf ofcourse by B ref. ( Because C IS-A B)

In Nutshell , you cannot say to your father, Hey access your pf , but Father can say to its child "Hey Son/daughter access your pf ".

Cheers!!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Sahil Kapoor wrote:
In Nutshell , you cannot say to your father, Hey access your pf , but Father can say to its child "Hey Son/daughter access your pf ".


I don't think that's a good way to think about it. I think it is better to think that only objects that IS-A YOU can be accessed by YOU.

In the case of B, both B and C references are guaranteed to be IS-A B instances. And hence, both objects are guaranteed to have the same permissions of B, which means that both objects can be accessed by B.

In the case of C, while a C reference IS-A B, the reverse is not true. In fact, it could be an entirely new class type, say D, that inherits from B, that is in a completely different package. So, the B references is not guaranteed to hold an objects that IS-A C, and hence, can't be accessed by C.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected Modifier still a challenge