0

related question: reference to valueOf is ambiguous

Has this situation the same background as why we can only extend one class, because if we could extend two classes, for all the static fields etc, the naming could cause the same problems?

From the wiki:

its increased complexity and ambiguity in situations such as the "diamond problem", where it may be ambiguous as to which parent class a particular feature is inherited from if more than one parent class implements said feature

Community
  • 1
  • 1
WonderWorld
  • 956
  • 1
  • 8
  • 18
  • possible duplicate of [Why is Multiple Inheritance not allowed in Java or C#?](http://stackoverflow.com/questions/995255/why-is-multiple-inheritance-not-allowed-in-java-or-c). Actually in Java 8 you can implement multiple interfaces which have the same default method so no multiple inheritance is more of a design decision than "it cannot be done". – Mateusz Dymczyk Mar 31 '15 at 14:08
  • Is your question why is multiple inheritance not allowed in java? – Brett Okken Mar 31 '15 at 14:09
  • @BrettOkken no, the question is: is one the problems you run into with multiple inheritance, also one of the problems when using 2 static imports who share the same methods. (in java) – WonderWorld Mar 31 '15 at 14:23
  • @MateuszDymczyk Multiple inheritance of state (which is the most complicated to resolve) is still forbidden though. – biziclop Mar 31 '15 at 15:43

3 Answers3

0

Regarding the specific problem that is linked in the question, I think the answer is no.

The original question is about the fact that static imports of valueOf() will collide when one tries to import Float.valueOf() and Integer.valueOf() within the same Java class.

But this collision has nothing to do with multiple inheritance. The point is: if you consider these methods in the context of their "enclosing" class, they are of course "different". But when that context is "removed", they can no longer be distinguished.

In other words: there is no "multiple inheritance" involved when statically importing methods. Just think of a static import as a mechanism to define some kind of "alias definition". And you can't use "the same" alias to mean two different things.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I know it has nothing to do with multiple inheritance ,i meant more that the problem you can run into with using 2 static imports, the ambiguous error, if that one of the reasons why we can't use multiple inheritance also, because it's not hard to imagine that it would. – WonderWorld Mar 31 '15 at 14:18
0

Static methods introduce no new/different issues with multiple inheritance. If anything they are slightly less complicated as they are not polymorphic.

Community
  • 1
  • 1
Brett Okken
  • 6,210
  • 1
  • 19
  • 25
0

No, the diamond problem is about a lot more than this.

Yes, it is true that multiple inheritance would cause name clashes that need resolving. This can easily be done by adding context and an example is how default methods handle this scenario. Your problem is quite similar to this.

But there is a more interesting part to the diamond problem: what happens with the fields (not the methods) that are inherited from a common ancestor on two different routes? This is what languages that allow multiple inheritance in its entirety need to answer. (And they do it in various ways.)

biziclop
  • 48,926
  • 12
  • 77
  • 104
  • I think there is no avoiding that at some point you need to exactly specify where something needs to go. exactly... – WonderWorld Mar 31 '15 at 18:06
  • @WonderWorld Yes, but as I said, that's not the crucial part of the diamond problem. Or to refer back to your question, that's not why Java went with no multiple inheritance of state (only of type, and more recently, behaviour). – biziclop Mar 31 '15 at 21:23