• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

packages and protected access

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I seem to be having some problems understanding access.
I've got some code taken from the OCA book and made some change to it.

and


The code as it is doesn't compile, because apparently this line generates an error. Now, Gosling extends Bird and they sit in a different package, so I don't have package private access, but floatInWater() is of access protected so I would have thought that because Gosling extends Bird I could create an object of type Bird inside Gosling and access that protected method even from another package (if I change the access modifier of floatInWater() to public the code runs. I thought that the point of a protected access was to allow access from a subclass regardless of package
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot about the "responsible for implementation" requirement...

https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.2

Most of the time, subclasses access protected fields/methods via the this reference, so, this issue rarely comes up (and this is why line 7 works) .... but ... in order to access a protected field, your class need to be responsible of the implementation.

This means that the reference needs to be of a type, that IS-A the class, where the code accessing the class, is located.

Henry

 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, I gave it a read, not sure I understood the whole thing though. I guess I'm a bit confused because I'm creating an object of a different class, different than the one inside which I'm declaring the object, that is
 
Henry Wong
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jason Attin wrote: I guess I'm a bit confused because I'm creating an object of a different class, different than the one inside which I'm declaring the object, that is



Not sure what you are trying to say -- so, not sure of your confusion. Can you elaborate a bit?

Henry
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what I meant was it's probably the fact that in class Gosling I'm trying to create an object of type Bird, and because Gosling extends Bird I thought I could access its protected method even if they sit inside the same package. I'm not too sure I understand that responsible for implementation requirement in practice
 
Marshal
Posts: 70308
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider it as meaning part of the object. The static method you had does not constitute part of the object.
 
Henry Wong
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jason Attin wrote: I'm not too sure I understand that responsible for implementation requirement in practice



Let me try to elaborate a bit...

First of all, where is the code that is accessing the protected field? Well, in this case, it is a static method of the Gosling class. To be specific, it is the main() method of the Gosling class.

So, the "code" is part of the Gosling class. The "responsible for the implementation" rule states that it is allowed to access all protected fields of super classes, provided that it too are IS-A Gosling instances. This means that, if you had done this in the main() method...
it works. Notice that you are still accessing the super class protected field, but in this case, the reference IS-A Gosling instance, and this "code" is responsible for Gosling instances, so hence, this would be allowed.

In other words, you can access super class protected fields (that are declared in classes in different packages), it is just that the reference must be IS-A Gosling; so, the object type must be Gosling class type or one of its sub class types.

Henry
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic