Question

Don't get me wrong. I am searching for this answer quite some time. And frankly speaking I could not get a satisfying answer anywhere. In a lot of places - Its mentioned that MI poses the following problem big time. I have two classes A and B, and one class C is inherited from both A and B, and both A,B declares a method , say foo(int i), and I forget to declare a foo(int i) in the C class, then compiler does not know which foo(int) to call when i issue something like this -

C c = new C();
c.foo(i);

But can we overcome in case we use multiple interfaces instead of inheritance through multiple classes? Do we call implementation of multiple interfaces as multiple inheritance? How the above problem is handled by compiler if i use multiple interfaces?

also, regarding - when should i use an interface and when should i do the same through abstract class - Does that only depend on such a thought that -

If the requirement changes a lot and quite often, then I will implement the same using interfaces and otherwise abstract classes would be my choice? I want to know are there any other reason that anyone wants to mention. These are pretty basic concepts of the OOO and I think I'm missing something to get a considerable progress in my learning the same. Sorry to make this question a bit long.

Was it helpful?

Solution

The problems with multiple inheritance have been the reasons to forbid it and enforce the usage of interfaces, as java has been designed. Since many real-world use cases of multiple inheritance are solvable with interfaces without the problems of MI.

If you search the net, you will be presented with examples, where multiple inheritance is called the only real solution. It seems possible to write programs without it, as you can see in java-solutions.

At least in those cases where it really is about implementing an interface I would suggest implementing an interface.

Implementing multiple interfaces is free of those problems, because it is only a contract what data / methods you have to provide, and not how.

So interface is about contracts, and inheritance is about common data and methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top