javainterfacemultiple-inheritance

Do interfaces solve the "deadly diamond of death" issue?


Do interfaces solve the deadly diamond of death problem?

I don't think so, for example:

// A class implementing two interfaces Interface1 and Interface2.
// Interface1 has int x=10 and Interface2 has int x = 20

public class MultipleInterface implements Interface1, Interface2{

    public void getX(){
        System.out.println(x);
    }
}

Here we get an ambiguous x.

Though interfaces are a good way for solving method ambiguity, I guess they fail in the case of variables?

Am I correct? If I am missing something, enlighten me.


Solution

  • Java prevents multiple concrete/abstract class inheritance, but not multiple interface inheritance. With multiple interface inheritance you inherit abstract methods, not implementation. See this post with a good explanation and examples: https://web.archive.org/web/20120724032720/http://www.tech-knowledgy.com/interfaces-sovles-diamond-death/