I am currently perusing through Jdk 8 and I came across the feature where you have multiple interfaces that overlap with the same method signature for a default method, the compiler will throw an error if you try to have one class implement both interfaces, to prevent the problem of ambigious methods with multiple inheritance.My question is, why can't java also allow multiple inheritance with classes like it does with interfaces, and solve the ambigious field and method problem that results from multiple inheritances by simply throwing an exception like it does when implementing multiple interfaces that have identical default method signatures?
Why doesn't java allow multiple inheritance of classes when it allows multiple inheritance of interfaces?
java;multiple inheritance
Partially, this is historical. It would be much more difficult to add multiple inheritance now. For example, the Class.getSuperclass() method would no longer make sense, as a class might have multiple superclasses. Any code that depends on the single-inheritance nature of Java would break.Fields are more problematic for multiple inheritance than methods. Consider the case that you want to inherit from two superclasses each of which have a foobar field. What happens? You propose that the compiler simply reject this. But this would mean that there were certain pairs of classes that couldn't be inherited together just because they happened to use the same field name. This is different from methods, because for methods you can simply override the method with the desired behavior.Of course, you could come up with some semantics for multiple inheritance to make fields work properly. However, it would end up introducing a fair amount of additional complexity. In the eyes of java designers, multiple inheritence simply is not useful enough to warrant the additional complexity.