English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

6 answers

Because its object oriented structure you can't extend more than one class, instead you implement interfaces.
Think on this: you have a class named "Falcon" and other one named "Duck", you can't inherit characteristics of a falcon from a duck, instead you create a super class named "Bird" so, "Falcon" and "Duck" can inherit some their own characteristics from "Bird". But if you need some characteristic like "owner"; an "owner" is not part of "Bird" class, but you need an owner, so, you implement it like an Owner interface and "Owner" interface can be applied to other classes for example: cars, houses,....
Hope my explanation was good enough
Good luck

2006-08-29 09:38:11 · answer #1 · answered by sonfarX 4 · 0 0

Java was specifically designed to avoid most (if not all unfortunately) of C++'s problems. And multiple inhereritence is one of them.

Why is multiple inhereritence a problem? What happens when you multiple inherit and both classes have the same parent class somewhere in the inheritence tree? This will always the case in Java, since everything inherits implicitly from Object. Still don't see the problem?

When a class is instantiated, memory is allocated for it, so there would be memory allocted for two Object instances, with whatever class variables it has assigned to it. So when you set a properties of the Object object, which one gets set?

I know that C++ has some rules regarding this, but they are very confusing, so Java sought to rectify this confusion by not allowing multiple inheritence. And to tell you the truth, at first I was very stressed out when I tried to do stuff in Java where I thought multiple inheritence would be necessary. But after rethinking the way I code, now it never even comes up (and I do a LOT of Java coding).

Just rememeber MVC, separation of interests, loosely coupled objects, and other best practices and you will find that the multiple inheritence "issue" is a non-issue. The JDK is written using these patterns; Swing is really good to study in how it does callbacks (Listeners) to achieve loosely coupled code. MVC is all over the place in Swing, as is separation of interests.

And there are lots of other patterns in Java, look at the I/O classes (Reader, Writer) to see the decorator pattern in action.

The more you think in Java, the more you will like it, I promise. Especially if you are an experienced C++ programmer. Just getting rid of the damn header files is more then worth the changeover.

2006-08-29 07:50:42 · answer #2 · answered by Violin Lover 2 · 1 0

Java provides multiple inheritance of interfaces but not classes. This can be a limiting factor when the parent of a class already exists and a new superclass should be added, which often occurs during programming. The workaround in Java is typically association, or to delegate to a member of the class rather than inherit. Wrapper methods are defined in the class which simply delegate to the member. The class then implements the required interface. This is problematic when the new class or interface adds methods, forcing the class to change whenever one of its interfaces or classes change. This does not occur with inheritance, since all new methods are inherited. This can be a highly annoying change control issue in a team environment.

2016-03-27 00:18:18 · answer #3 · answered by ? 4 · 0 0

Multiple inheritance has the inherent problem of ambiquity error. A method with the same signature could exist in more than one of the classes inherited by the derived class. In such a situation at runtime when the method is called using an object of the derived class it would not be able to make out to call the method in which of the base class. Java with a view to avoid this ambiquity does not support multiple inheritance.
In case of interfaces, though the same method signature may be available in more than one interface but there is only one implementation of it, the class that implements the interface(s).

2006-08-29 07:36:53 · answer #4 · answered by 2_b_or_not_2_b 3 · 0 0

Java does not let you directly extend two classes, but you can do multiple inheritance through a combination of inheritance, composition and delegation.

2006-08-29 10:10:13 · answer #5 · answered by Juice 2 · 0 0

It's usually to avoid the "deadly diamond" problem, where you can inherit something twice.To reduce the complexity and simplify the language,multiple inheritance is not supported in java. goo.gl/hDC6Hh

2016-08-23 20:19:12 · answer #6 · answered by Mack 1 · 0 0

fedest.com, questions and answers