-5

In java, in a subclass, how can super() or non-private methods that were defined in the superclass access private members of the superclass,

private members are not inherited in the subclass, therefore when we instantiate the subclass, private members are not instantiated, i.e. they don't exist, how can you access something that does not exist?

Abhishek
  • 21
  • 1
  • 2
    Yes, of course they are instantiated. They may be necessary for the correct functioning of the superclass. – Andy Turner Apr 12 '16 at 11:55
  • You have a fundamental misunderstanding. Private is an access modifier. The private members *are* inherited. – Dave Apr 12 '16 at 11:56
  • You could try printing variables from both classes and just see what works and what doesn't, what is null and what isn't. No need to ask about it here. – takendarkk Apr 12 '16 at 11:56
  • Yes they are but only visible in the super class! – Rene M. Apr 12 '16 at 11:57
  • @Dave: The JLS disagrees with you: "A class inherits from its direct superclass and direct superinterfaces all the non-private fields of the superclass and superinterfaces that are both accessible to code in the class and not hidden by a declaration in the class." It really depends on what *exactly* you mean by "inherits". – Jon Skeet Apr 12 '16 at 11:58
  • This is quite a good answer. http://stackoverflow.com/a/4716335/3973077 – Paul Boddington Apr 12 '16 at 12:01
  • @PaulBoddington: Yup, that looks close enough to a duplicate to make sense to close this one. I think it's a shame that this question got so many downvotes though... – Jon Skeet Apr 12 '16 at 12:01
  • Here's an example. If you run it, you can clearly see that it does exist. You seem to have misunderstood a fundamental concept. http://ideone.com/aGipof – Norsk Apr 12 '16 at 12:02
  • @JonSkeet People downvote users with 1 rep far too easily in my opinion. This is a good question, but I guess it could have been googled. – Paul Boddington Apr 12 '16 at 12:03
  • 1
    @PaulBoddington: Given the level of confusion shown in the comments, I'm not sure I'd trust that the results of such a search would be accurate :( – Jon Skeet Apr 12 '16 at 12:04

1 Answers1

2

The private fields aren't inherited, but they do exist. It really depends on what you mean by "inherited" here - and the JLS (e.g. in 8.2) is - I believe - referring to which members can be looked up by member resolution with respect to the subclass. Private members can't be resolved in that sense, but the fields still exist.

The state of an instance of a subclass consists of all the fields declared throughout the entire inheritance chain.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for the reply Jon, what is "inherited" supposed to mean? – Abhishek Apr 12 '16 at 15:31
  • @Abhishek: "supposed" in what sense? As I've said, it looks like the JLS is using it to mean the members that can be resolved by name for that subclass. (You could resolve a member that's inaccessible, but determine that later, for example.) I suggest you follow the link to the duplicate question for a lot more detail though. – Jon Skeet Apr 12 '16 at 15:36