• 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

Unexpected access to protected member

 
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anybody enlighten me as to how this is possible? I was making sure I understood the material from k&b p25, hence my package names.





As it's in a different package, I was expecting Horse to be able to access Animal.hello() only via inheritance. However, at line 08, Horse code makes a new Horse instance and accesses hello() via the dot operator. To my surprise, the code compiles & runs without error.

 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The code in question is:


I took it out of the package, because that shouldn't matter here. You are making the call from a subclass of Animal. (Horse). Since it is being called from in a subclass, it can access protected methods. If that line of code were in MyTest, it would fail because then it wouldn't be in a subclass (or the same package.)
 
Richard Hayward
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:
You are making the call from a subclass of Animal. (Horse). Since it is being called from in a subclass, it can access protected methods.



Well, here's code from k&b p35. It looks to me like it's doing the exact same thing. Code in a child instance attempts to invoke a protected method on an instance of the parent class, by using the dot operator.





This time, it behaves as I was expecting. The compiler gives an error:

p035\other\Child.java:9: error: x has protected access in Parent
System.out.println("x in Parent is " + p.x); //compile failure, x is protected in Parent
^
1 error



How is the situation different?
 
Jeanne Boyarsky
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's expand the Horse/Animal one a tiny bit.



h.hello() is ok because it is a method on the current class. a.hello() is not because it is in the same package. Granted, we know they are the exact same class. But from the compiler's point of view, the type in the declaration is all that matters.
 
author
Posts: 23951
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

Jeanne Boyarsky wrote:Let's expand the Horse/Animal one a tiny bit.

h.hello() is ok because it is a method on the current class. a.hello() is not because it is in the same package. Granted, we know they are the exact same class. But from the compiler's point of view, the type in the declaration is all that matters.



Perhaps we should expand the example a bit more.

First, as already mentioned, the access modifiers apply at a class level. This means that if something is private or protected, it is private or protected to the class. This means that code in a class can access protected members of other instances (and not just the this instance).

Second, there is another rule that applies to the protected modifier. For code in a class to access a protected member that is inherited from the super class, the code must be accessing an instance that it is responsible for. This means that for the Horse class to access a protected member of the Animal class, the instance used must be a Horse class (or an instance that IS-A Horse class).

Henry
 
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Second, there is another rule that applies to the protected modifier. For code in a class to access a protected member that is inherited from the super class, the code must be accessing an instance that it is responsible for. This means that for the Horse class to access a protected member of the Animal class, the instance used must be a Horse class (or an instance that IS-A Horse class).


I think you mean the "reference used" and not "instance used". For example,




This makes sense because access checking is done at compile time. So the class of the instance shouldn't matter.
HTH,
Paul.
 
Henry Wong
author
Posts: 23951
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

Paul Anilprem wrote:

Henry Wong wrote:
Second, there is another rule that applies to the protected modifier. For code in a class to access a protected member that is inherited from the super class, the code must be accessing an instance that it is responsible for. This means that for the Horse class to access a protected member of the Animal class, the instance used must be a Horse class (or an instance that IS-A Horse class).


I think you mean the "reference used" and not "instance used". For example,



Sorry. Of course, I mean that. We are talking compile time here.

BTW, for full details in the specification regarding protected access, see section 6.6.2:

http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2

Henry
 
Richard Hayward
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne, Henry & Paul, thank you all!

Your help & example code has helped me clarify my thinking in this area, where it seems to me, there are a few subtleties.

I'd not realized for example, that as Henry pointed out, private applies to classes, not instances. Makes code like this possible.




Richard
 
Forget Steve. Look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic