• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Protected and Inheritance

 
Greenhorn
Posts: 21
Firefox Browser C++ Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,

Im having trouble understand the "protected" keyword when applied to class members. I have gone through all the discussions here regarding that topic and I still can't figure it out.

The following examples illustrate the problem:





Whats the problem here? AFAIK, ExtendsProtectedClass has its own copy of "show()" and e1.show() should work.

I know it has something to do with this:
Sun language specification, 6.6.2-
"A Protected member 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."

What does "implementation of that object" mean? I can't seem to connect the dots. Any help will be appreicated.

Thanks.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

implementation of that object



this means you can access a protected member of a class through its own object outside the package. you can't access it through the subclass object of the class which actually has the protected member or field.
According to your example, you can't access show() of ProtectedClass through ExtendsProtectedClass object outside package first. If you need to access
show() in package second you will have to create object of ProtectedClass.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Pujari wrote:I know it has something to do with this:
Sun language specification, 6.6.2-
"A Protected member 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."

What does "implementation of that object" mean? I can't seem to connect the dots. Any help will be appreicated.


Just to add to what Nomaan said, this might help:
Winston
 
Nikhil Pujari
Greenhorn
Posts: 21
Firefox Browser C++ Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nomaan Butt wrote:

implementation of that object



this means you can access a protected member of a class through its own object outside the package. you can't access it through the subclass object of the class which actually has the protected member or field.
According to your example, you can't access show() of ProtectedClass through ExtendsProtectedClass object outside package first. If you need to access
show() in package second you will have to create object of ProtectedClass.



That does not work. This is what you said:



@Winston:
Your code works and I understand why - Its because we are accessing the inherited copy of "i" from within "ExtendsProtectedClass".

But my question is, since "UsesExtendedClass" is in the same package as "ExtendsProtectedClass", it should be able to access the protected members of "ExtendsProtectedClass", even if "UsesExtendedClass" does not inherit from/extend "ExtendsProtectedClass". That kind of access is allowed, according to the rulebook. But why does it throw up an error in this case?

 
Nikhil Pujari
Greenhorn
Posts: 21
Firefox Browser C++ Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the answer. Its page 27 on the Sun Certified Java programmers book by Kathy Sierra and Bert Bates - (310-065). I am not sure if I am allowed to quote the entire paragraph, so I wont. What it says is this- consider a protected member a1 in class A in package Z. Class B in package Y extends class A. Package Y contains Class C which extends class B. Package Y also contains class D (which does not extend any class).

Class B and Class C will inherit a1 and can access their a1 through their object. Class D cannot access a1, even though its in the same package as D. This is an exception to the rule that says - Protected members can be accessed by any class in the same package.

The above does not hold if all the four classes are in the same package. In this case, D can access a1.

Thank you.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Pujari wrote:...This is an exception to the rule that says - Protected members can be accessed by any class in the same package.
The above does not hold if all the four classes are in the same package. In this case, D can access a1.


Which I think you'll find is precisely what Nomaan said.

It's maybe worth remembering that Class B doesn't inherit a copy of a1, it inherits a1 itself; because a class B is a class A.
However, a1 still belongs to class A, because that's where it is defined; and that is why class D can't access it.

Winston
 
Nikhil Pujari
Greenhorn
Posts: 21
Firefox Browser C++ Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
It's maybe worth remembering that Class B doesn't inherit a copy of a1, it inherits a1 itself; because a class B is a class A.
However, a1 still belongs to class A, because that's where it is defined; and that is why class D can't access it.



Does that mean a1 is shared by objects of class A and class B? I thought an object of class B had a separate a1 than that of class A, and if you modified a1 in an object of class B, then the other one would not be affected.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Pujari wrote:

Winston Gutkowski wrote:
It's maybe worth remembering that Class B doesn't inherit a copy of a1, it inherits a1 itself; because a class B is a class A.
However, a1 still belongs to class A, because that's where it is defined; and that is why class D can't access it.



Does that mean a1 is shared by objects of class A and class B? I thought an object of class B had a separate a1 than that of class A, and if you modified a1 in an object of class B, then the other one would not be affected.



Suppose we talk about something concrete to make this discussion more vivid? Imagine there is a class Car that has a protected instance field of type Engine. This means that every instance of Car has its own instance of Engine. Now, suppose there is a class TeslaRoadster that extends Car. By virtue of inheritance, every instance of TeslaRoadster has its own instance of Engine.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic