Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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

Inheriting two Fields with the same Name

 
Ranch Hand
Posts: 238
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm taking a look at the JLS at "http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.1.3" in the section labeled "8.3 Field Declarations" eight paragraphs down where it says, "It is possible for a class to inherit more than one field with the same name (8.3.3.3). Such a situation does not in itself cause a compile-time error. However, any attempt within the body of the class to refer to any such field by its simple name will result in a compile-time error, because such a reference is ambiguous."

To illustrate this I created four Java files, namely "Abc.java":

"Def.java":

"Ghi.java":

and "Driver.java":

Note that class {Ghi} inherits both the {common} field from {Abc} and the {common} field from {Def}, so this seems to fit the "more than one field with the same name" description in the JLS. Sure enough, when I type in "javac Driver.java", all the files compile successfully, and when I type in "java Driver 314 159 265 358 979" I get results:

rst: 159
uvw: 358
xyz: 979

But the JLS seems to be saying that if I modify "Ghi.java" like so:

and then try to compile "Ghi.java" I should get a compilation error, since my reference to {common} in method {anal()} is ambiguous; it could be either the {common} field from {Abc} or the {common} field from {Def}. However when I type in "javac Ghi.java" it compiles just fine, and when I type in that same string, "java Driver 314 159 265 358 979" I get results:

rst: 159
uvw: 358
xyz: 979

common: 265

Is this a bug with my compiler? If this isn't a case of what this paragraph in section 8.3 says shouldn't compile, then what is?

Kevin Simonson
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS actually speaks of situation where the fields are inherited from the same "level" of predecessors (this implies that at least one of them is an interface):

The same could happen with two interfaces, of course. In this case, there really is ambiguity if unqualified name is used, as it is not clear which field you want to use.

In your example, the field in the superclass has been hidden by the field in the subclass. Any class inheriting from the subclass inherits the subclass' version of the field. It is allowed by the language, but I'd say such situation constitutes a serious design flaw.
 
Kevin Simonson
Ranch Hand
Posts: 238
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vajsar wrote:The same could happen with two interfaces, of course. In this case, there really is ambiguity if unqualified name is used, as it is not clear which field you want to use.

In your example, the field in the superclass has been hidden by the field in the subclass. Any class inheriting from the subclass inherits the subclass' version of the field. It is allowed by the language, but I'd say such situation constitutes a serious design flaw.


Thanks! This explanation helps a lot.
 
I don't even know how to spell CIA. But this tiny ad does:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic