aspose file tools*
The moose likes Beginning Java and the fly likes Protected and Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Protected and Inheritance" Watch "Protected and Inheritance" New topic
Author

Protected and Inheritance

Nikhil Pujari
Greenhorn

Joined: Oct 31, 2011
Posts: 21

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.


Einstein: A problem cannot be solved in the same state of mind in which it was created.
More motivation: http://www.youtube.com/watch?v=_7xfyDjCFqs
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Nikhil Pujari
Greenhorn

Joined: Oct 31, 2011
Posts: 21

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

Joined: Oct 31, 2011
Posts: 21

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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: Oct 31, 2011
Posts: 21

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.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected and Inheritance