problem with deadly diamond of death

deepak kushwaha

Ranch Hand

Posts: 33

posted 7 years ago

if have read that interfaces can extend any number of classes! isnt it going to something same as deadly diamond of dead. i mean what if some one extended an inteface with two classes which have two same methods. which method will get implemented?

interfaces can only extend other interfaces, not classes. Since interfaces can't implement any methods, there are no questions about where the implementation of a method comes from -- it can only come from a class that eventually implements the interface.

If a class implements 2 interfaces, both having similar method declarations , then the class should implement the method only once.

The compiler would not complain. Both the interfaces are satisfied because their method is implemented in class.

Hope this clarifies

deepak kushwaha

Ranch Hand

Posts: 33

posted 7 years ago

well what if i created my own interface lets say A and i define some methods in it, now again i create an interface B that extend two interfaces lets say A and C (i haven't created that one).now by chance there is a method which have the same name , return type and the argument in both the interfaces A and C, but they are doing two different thing in the method implementation. now i made a class which implements interface B and i called that same method. now which method will get called?

In this case, although syntactically your code will be correct, you violate the contract of one of the interfaces (semantically). It's therefore a sign of bad design, and you'll need to refactor your code.