Java inheritance

后端 未结 6 1118
旧巷少年郎
旧巷少年郎 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 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.

提交回复
热议问题