• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

A fuzzy question...

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an overridding method cannot have a more restrictive access level, why the inherited superclass-in-different-package protected members are treated as private for the other classes in the subclass package? I fully understand the concept around protected, but why this inconsistent behavior? Are they really inherited members or they are created in some weird way "at the fly" by the compiler/JVM?
 
Sheriff
Posts: 6959
1317
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Marco wrote:...why the inherited superclass-in-different-package protected members are treated as private for the other classes in the subclass package?



Mmmm, sorry but I couldn't understand the exact meaning of this. Can you provide an example?
 
author
Posts: 23928
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
I am assuming that you mean "fuzzy".... as the word "fussy" doesn't really apply here (in a way that makes sense)...

Henry
 
David Marco
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Henry, my english level is not sufficient yet




Dog inherits method() (which has protected acces in superclass) but from Person perspective Dog.method() is private (more restrictive acces level).
 
Henry Wong
author
Posts: 23928
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

... but from Person perspective Dog.method() is private (more restrictive acces level).



From the Person perspective, it can't access the superclass method() either, as it is in a different package.

Henry
 
Devaka Cooray
Sheriff
Posts: 6959
1317
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Marco wrote:If an overridding method cannot have a more restrictive access level, why the inherited superclass-in-different-package protected members are treated as private for the other classes in the subclass package?



Is their anything related to the "overriding" here? I couldn't see any overridden method in your last program.

Devaka.
 
David Marco
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Devaka you open my eyes!! I'm not overriding, but inheriting, so the overriding rules aren't broken. But I still cannot undestand why the language says that Dog.method() cannot be visible to Person!

From the Person perspective, it can't access the superclass method() either, as it is in a different package.


But I'm trying to call Dog.method(), not Animal.method()... The K&B book say:

Remember, if a subclass inherits a member, it's
exactly as if the subclass actually declared the member itself. In other words, if a
subclass inherits a member, the subclass has the member.


Is this accurate?
 
Henry Wong
author
Posts: 23928
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

Another way to think about it is this... The Animal class intended the method to be accessable by (1) its subclasses and (2) classes within the same package. It never included, the classes within the same package of its subclasses.

Henry
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David Marco

protected is only visibility in the same package or for those who extends.
 
David Marco
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try to expose my doubt one more time in a different way:

The K&B book says:

Remember, if a subclass inherits a member, it's exactly as if the subclass actually declared the member itself.
In other words, if a subclass inherits a member, the subclass has the member.


so:


If the inherited member is declared implicitly as protected in the subclass, why it behaves like private (more restrictive) for the rest of classes in package B? Where is the trick? THIS IS MY QUESTION!! I know, as I said in the first post, who protected access works. It's clear to me. But not why!

Thanks in advance...
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,

The K&B book says:

Remember, if a subclass inherits a member, it's exactly as if the subclass actually declared the member itself.
In other words, if a subclass inherits a member, the subclass has the member.


That's true. BUT it doesn't say anything about which access modifier is used. Which means, from this quote alone, we don't know which access modifier is used and we have to go back to the definition of the protected modifier. So your statement

If the inherited member is declared implicitly as protected in the subclass ...


is wrong because of the definition of protected (source: KB):

protected members can be accessed by other classes in the same package, plus subclasses regardless of package



I hope I could help
Bob
 
David Marco
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bob this is what I wanted to hear. Thanks to everyone.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But if you have a third class in another package and that one extends your subclass, the protected member will still be accessible to that third class.
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic