• 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

Inheritance with Nested classes

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
On my way to get OCPJP 8, I started the preparation. My problem is about the following sentence:
"A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass."

I read it here: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Please, help me to clearly understand it.
 
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

I don't know why the document pointed it out... but I guess it was done for completeness.

One. Access modifiers are for everything within the top level class. So, private mean private to everything within the top level class. Private fields and methods, no matter where they are, either as direct members of the top level class, or any nested/inner class, can be accessed by any code, either as part of the top level class, or any nested/inner class.

Two. In order to inherit from a superclass, you need access to it. So, if you want to inherit from a nested / inner class, then you need it to be accessible -- ie. public or protected.

And finally, three, the key word in the sentence, is "indirect". Any private member can be assess via non-private setters and getters. So, the subclass can access the superclass private fields only indirectly.

... put these three points together, you get that explanation.

Hope this helps,
Henry
 
Hervé Lionel Ondoua
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. This is what clearly what I understand.

But from the last sentence, this is what I understand and it does work:

given,
public class OuterClass{
private str= "From Outer"

protected class InnerClass{
}
}

public class Main extends Outer.InnerClass{
}
 
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

Hervé Lionel Ondoua wrote:
But from the last sentence, this is what I understand and it does work:



The last sentence was referring to indirectly accessing the superclass (that happens to be a nested class) private fields from the subclass. Your example doesn't have any accessing (direct or indirect) from the subclass. In fact, there isn't any code at all in the subclass.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic