Java inheritance

后端 未结 6 998
旧巷少年郎
旧巷少年郎 2020-12-22 06:09

Why we can not extend more than one class in java? can anyone clearify this point please.

相关标签:
6条回答
  • 2020-12-22 06:45

    Long story in short,

    That's why we need interfaces for. Combining interfaces together is better than an over-weight base class.

    0 讨论(0)
  • 2020-12-22 06:49

    That's a design decision - see e.g. Why not multiple inheritance for the rationale. Short reason: MI is complex, Java doesn't want to be complex.

    0 讨论(0)
  • 2020-12-22 06:58

    How about method name conflicts, for example when they same method exists in both super classes.

    0 讨论(0)
  • 2020-12-22 07:04

    Have a look at the Diamond Problem

    0 讨论(0)
  • 2020-12-22 07:04

    Because java does not support multiple inheritance. Here are few articles explaining why

    1. article
    2. article
    0 讨论(0)
  • 2020-12-22 07:06

    James Gosling / Henry McGilton:

    Multiple inheritance - and all the problems it generates - was discarded from Java. The desirable features of multiple inheritance are provided by interfaces - conceptually similar to Objective C protocols. An interface is not a definition of a class. Rather, it's a definition of a set of methods that one or more classes will implement. An important issue of interfaces is that they declare only methods and constants. Variables may not be defined in interfaces.

    In other words, it eliminates the problems of ambiguity ("A" inherits "B" and "C", "B" and "C" inherits "D" - is "A" "D"?).

    Instead of multiple class inheritanse, in Java you should use multiple interface inheritance. Interfaces describes behavior of object and there are no ambiguities.

    0 讨论(0)
提交回复
热议问题