• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Protected Modifier still a challenge

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic