aspose file tools*
The moose likes Java in General and the fly likes Inheriting two Fields with the same Name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inheriting two Fields with the same Name" Watch "Inheriting two Fields with the same Name" New topic
Author

Inheriting two Fields with the same Name

Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 114
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
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

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

Joined: Oct 22, 2011
Posts: 114
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.
 
jQuery in Action, 2nd edition
 
subject: Inheriting two Fields with the same Name