user12345
user12345

Reputation: 19

Java inheritance

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

Upvotes: 0

Views: 371

Answers (6)

CloudyMarble
CloudyMarble

Reputation: 37566

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

Upvotes: 0

ninja
ninja

Reputation: 2968

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.

Upvotes: 3

J.S. Taylor
J.S. Taylor

Reputation: 761

Long story in short,

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

Upvotes: 0

Mojo Risin
Mojo Risin

Reputation: 8142

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

  1. article
  2. article

Upvotes: 0

Erik
Erik

Reputation: 91270

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.

Upvotes: 6

Thomas Jungblut
Thomas Jungblut

Reputation: 20969

Have a look at the Diamond Problem

Upvotes: 1

Related Questions