Multiple Inheritance Ambiguity with Interface

Razib

We all know about the diamond problem regarding multiple inheritance -

   A
  / \
 B   C
  \ / 
   D

This problem describe an ambiguous situation for class D. If class A has a method and both/either of B and/or C override the method then which version of method does D override?

Is this problem also applicable for interfaces in Java? If not, how do Java interfaces overcome this problem?

Daniel Pryden

The diamond problem only applies to implementation inheritance (extends in all versions of Java prior to Java 8). It doesn't apply to API inheritance (implements in all versions of Java prior to Java 8).

Since interface methods with matching type signatures are compatible, there is no diamond problem if you inherit the same method signature twice: matching method signatures simply coalesce instead. (And if the type signatures aren't the same, then you don't have the diamond problem either.)

In Java 7 and below, the only way to inherit implementation code was via the extends keyword, which restricts to at most one parent. Therefore there is no multiple implementation inheritance and the diamond problem does not exist.

Java 8 adds a new wrinkle because it allows interfaces to have implementation code. It still escapes the diamond problem by simply falling back to the previous behavior (no implementation inheritance) when you're implementing multiple interfaces with methods that have matching signatures.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ambiguity with [] operator and multiple inheritance

From Dev

About multiple inheritance and ambiguity

From Dev

Multiple Inheritance Virtual Call Ambiguity

From Dev

Multiple inheritance with no function overriding. Why the ambiguity

From Dev

C++ template subclass and multiple inheritance ambiguity

From Dev

Multiple inheritance with no function overriding. Why the ambiguity

From Dev

How do references resolve the ambiguity in multiple inheritance?

From Dev

Multiple inheritance and interface methods

From Dev

Problems in multiple interface inheritance

From Dev

Jackson and multiple interface inheritance

From Dev

Jackson and multiple interface inheritance

From Dev

C++ multiple inheritance static function call ambiguity

From Dev

C++ multiple inheritance static function call ambiguity

From Dev

Multiple Interface inheritance in C#

From Dev

Multiple inheritance with interface in C++

From Dev

Multiple inheritance, inheriting an interface and an implementation

From Dev

Runtime multiple inheritance with impromptu-interface

From Dev

Multiple templated interface inheritance name hiding

From Java

Do C# 8 default interface implementations allow for multiple inheritance

From Dev

Call an interface function from an unknown derived class (multiple inheritance)

From Dev

Typescript name collision/clash using multiple interface inheritance

From Dev

Call an interface function from an unknown derived class (multiple inheritance)

From Dev

Interface inheritance combined with class inheritance

From Dev

Interface inheritance - Issue with double inheritance

From Dev

Interface inheritance combined with class inheritance

From Dev

Dropbox Djinni - Interface Inheritance

From Dev

Interface inheritance without generics

From Dev

interface and inheritance in java

From Dev

Delphi - Interface inheritance with generics

Related Related

  1. 1

    Ambiguity with [] operator and multiple inheritance

  2. 2

    About multiple inheritance and ambiguity

  3. 3

    Multiple Inheritance Virtual Call Ambiguity

  4. 4

    Multiple inheritance with no function overriding. Why the ambiguity

  5. 5

    C++ template subclass and multiple inheritance ambiguity

  6. 6

    Multiple inheritance with no function overriding. Why the ambiguity

  7. 7

    How do references resolve the ambiguity in multiple inheritance?

  8. 8

    Multiple inheritance and interface methods

  9. 9

    Problems in multiple interface inheritance

  10. 10

    Jackson and multiple interface inheritance

  11. 11

    Jackson and multiple interface inheritance

  12. 12

    C++ multiple inheritance static function call ambiguity

  13. 13

    C++ multiple inheritance static function call ambiguity

  14. 14

    Multiple Interface inheritance in C#

  15. 15

    Multiple inheritance with interface in C++

  16. 16

    Multiple inheritance, inheriting an interface and an implementation

  17. 17

    Runtime multiple inheritance with impromptu-interface

  18. 18

    Multiple templated interface inheritance name hiding

  19. 19

    Do C# 8 default interface implementations allow for multiple inheritance

  20. 20

    Call an interface function from an unknown derived class (multiple inheritance)

  21. 21

    Typescript name collision/clash using multiple interface inheritance

  22. 22

    Call an interface function from an unknown derived class (multiple inheritance)

  23. 23

    Interface inheritance combined with class inheritance

  24. 24

    Interface inheritance - Issue with double inheritance

  25. 25

    Interface inheritance combined with class inheritance

  26. 26

    Dropbox Djinni - Interface Inheritance

  27. 27

    Interface inheritance without generics

  28. 28

    interface and inheritance in java

  29. 29

    Delphi - Interface inheritance with generics

HotTag

Archive