line 20 shows an error
but if i make a change on line 10 as : interface abcd extends abc , then it compiles fine and gives the output (abcd) by executing abcd interface's version of method m().
can somebody explain what exactly is happening on the lines 10 and especially 20 ?
shambhavi sham wrote:can somebody explain what exactly is happening on the lines 10 and especially 20 ?
In the current code, class c2 has two versions of the method m(): one from each interface it implements. So iit's impossible for the compiler to know which version of the method m() must be used in class c2 and that's why you give the compiler. One possible solution would be to implement the method m() in class c2. And that's probably the most common used solution (because you can keep the current class and interface hierarchy).
If you change the interface hierarchy (let interface abcd extend from interface abc), you don't have two versions of the method anymore. The method m() in interface abc is overridden by the method m() in interface abcd. And that's why you don't get a compiler error anymore and the program can be executed.
shambhavi sham wrote:
but c2 class is inheriting from abc and abcd , so it will have one form abc and one from abcd right
Default methods are *not* a form of multiple inheritance. It may look like multiple inheritance, but it is not multiple inheritance.
If your class doesn't implement a method of the interface (or inherits it from its superclass), then the compiler will use the default method from the interface.
If there are multiple matching default methods, and there is no way to disambiguate, as in your first case, then you get a compiler error. If there are multiple matching default methods, and one is from the subinterface of the other interface, then the compiler will choose the one from the subinterface; as shown in your second case.