Code Java:
can you tell me what is the out put
and which interface will call when you want display ()
Printable View
Code Java:
can you tell me what is the out put
and which interface will call when you want display ()
No. Can you tell us? What happened when you wrote a test program to test this?
Assuming that's all the code, neither. You'll get compile errors.
If you're unsure what code's doing, test it out.
Note that an interface can't be "called", neither can a method in an interface be "called" (since all interface methods are abstract). Only the lowest implementation of that method (with respect to the actual object type) is called.
Also, this is what is referred to as "the diamond problem". (If you have no idea what that is, google it and come back.)
The diamond problem *usually* doesn't happen in Java, because there is no multiple inheritence. However, as you've shown here, you can implement multiple interfaces.
An interface doesn't care how you implement a method, as long as it's there. So it doesn't care if another interface also contains that method definition. It's up to you as a programmer to know what's going on.
Whether or not this is the diamond problem is debatable. Wikipedia says it isn't. But it can be confusing, especially if you want two functions to do completely different things. For example:
Code java:public class DiamondProblemTest { public interface Cowboy{ public void draw(); } public interface Artist{ public void draw(); } public static class Person implements Cowboy, Artist{ public void draw(){ //should I pull out a gun or a paintbrush? } } public static void main(String... args){ new Person().draw(); } }
The answer's obviously that your person needs to draw pictures with his bullets :P