0

This phrase form the Java 11 Language Specification (8.3 Field Declarations) is not clear for me

A class inherits from its direct superclass and direct superinterfaces all the nonprivate fields of the superclass and superinterfaces that are both accessible (§6.6) to code in the class and not hidden by a declaration in the class.

That is it is not clear if a name of the superclass is hidden by a declaration in the class does it mean that nonprivate fields are not inherited?

The part of the phrase "and not hidden by a declaration in the class" is confusing.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Maybe this has to do with masking / shadowing? If your sublcass declares a field or local variable with the same name as the one in the superclass, java prioritizes the most local definition by default, and you therefore "lose access" to the original. – Austin Schaefer Jun 17 '19 at 08:09
  • @AustinSchäfer I think that inheritance and hiding are two different things. – Vlad from Moscow Jun 17 '19 at 08:11
  • I would try a small example: create a super class with `String s = "s"` and subclass it with `String s = "t"`, and see which value it gives when you print to console. And they may be two different things, but their interplay could be why this ends up being mentioned. If my example doesn't work, then I'm wrong and it isn't the problem. – Austin Schaefer Jun 17 '19 at 08:11
  • @AustinSchäfer It is obvious that s of the subclass will be accessed. But how is it related to the phrase? – Vlad from Moscow Jun 17 '19 at 08:18
  • If the subclass variable masks the superclass variable, then it was "hidden by a declaration in the class (the subclass)." – Austin Schaefer Jun 17 '19 at 08:18
  • @AustinSchäfer I agree with this but it does not mean that nonprivate fields are not inherited. – Vlad from Moscow Jun 17 '19 at 08:21
  • Ok, no problem; it was only the first thing which came to my mind. – Austin Schaefer Jun 17 '19 at 08:22

1 Answers1

-1

Oh, I have understood the phrase. The problem is that English is not my native language.

The phrase means that if a field of the supertclass or a superinterface is for example hidden by a declaration of the subclass then according to the Java Specification that field is not inherited. Though in my opinion it looks strange.

But to prove my conclusion I can cite a comment for the example Example 8.3.1.1-2. Hiding of Class Variables. There is written the following

...because the declaration of x in class Test hides the definition of x in class Point, so class Test does not inherit the field x from its superclass Point.

Nevertheless the subclass Test can access the hidden field of the class Point. So the statement that the class Test does not inherit the field x looks strange for example for a C++ programmer.:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • It looks strange how? If it's hidden, how do you think it should appear in the subclass? – user207421 Jun 17 '19 at 11:01
  • @user207421 I pointed out that it looks strange for a C++ programmer. In C++ even if a member of a base class is hiden by a member of a derived class nevertheless the data member of the base class is inherited by the derived class. – Vlad from Moscow Jun 17 '19 at 11:23
  • @user207421 It seems you never dealt with C++. Logically if a derived class has acces to members of the base class then it is obvious that the derived class inherits the members of the base class. – Vlad from Moscow Jun 17 '19 at 11:43
  • 1
    It seems, you are confusing “inheritance”, “accessibility”, and even “has storage per instance” here. A subclass may *access* a field even if it is not *inherited*, e.g. via `super.name`, but it’s also possible to access, e.g. a `public` field via `variable.name` from an entirely different context. Being able to access a variable does not prove that the variable has been inherited. Neither does the absence of a possibility to access a variable prove that the variable (and its per-instance *storage*) did not exist. Most notably, the never-inherited `private` variables do, of course, still exist – Holger Dec 06 '21 at 15:00