2

Just for the curiosity I tried this:

public interface test1 {
    public Object method1(String str);
}

public interface test2 {
    public Object method1(String str);
}

public class Test implements test1, test2 {    
    public static void main(String... args) {

    }

    public Object method1(String str) {
        return new Object();
    }
}

I was expecting some sort of error on line public class Test implements test1, test2 { from Eclipse, but there was none.

  1. Why there is no compile time error since there is a collision of method names?
  2. How many methods are inherited in class Test?
  3. If the answer of the above question is 2, then exactly which method have we implemented?
  4. And at runtime, how come the properly implemented method is selected? and not the other one?
  5. And finally, is there a way to implement both? (I am almost sure that the answer is NO)

I know that from the design point of view, this may not happen in the real world situations, but as I have mentioned at the beginning, it just came to my mind and I tried it out.

Any clarification/reference is greatly appreciated.

Bhushan
  • 18,329
  • 31
  • 104
  • 137
  • 2
    What's the problem? Both interfaces promise that any class that implements them has a method `method1(String)`, and your `Test` class fulfills that promise. – Kerrek SB Dec 02 '11 at 15:10
  • 1
    it would be more problematic if you had interface1 { public int method1(String var); } and interface2 {public String method1(String var); }. Then you would get an error for not implementing one of them (and you are unable to resolve that.) – Angelo Fuchs Dec 02 '11 at 15:13
  • @Angelo Neuschitzer: but in that case there would have been no collision at all – Bhushan Dec 02 '11 at 15:15
  • 2
    @Bhushan: But you cannot have two methods with the same signature that return different data types. So you will never be able to implement both interfaces and get the code to compile. – Erick Robertson Dec 02 '11 at 15:18
  • Ohh..sorry I missed that. Got it now. Yes, that's a good point indeed. – Bhushan Dec 02 '11 at 15:22
  • 1
    Think of a method in an interface as saying to classes that implement it "you must have a method that looks like this". Test class implements that, saying effectively "here is that method" to both. – DJClayworth Dec 02 '11 at 15:36

3 Answers3

7

There is no collision. Both interfaces require a method called method1, and both are satisfied by the method1 in your example class. When you implement an interface, you are not inheriting any methods at all; you are only promising to implement the methods yourself.

For the answer to your final question, see this existing SO question:

Java - Method name collision in interface implementation

There is a proposal for Java 8 to allow interfaces to specify a default implementation, called Virtual Extension Methods. If that makes it into the language, it will be possible for two interfaces to specify different default implementations for the same method signature. If those interfaces are then implemented by one class then it will have to provide its own implementation (i.e. it will be equivalent to neither interface specifying a default) - see 3.3. Conflicting defaults.

Community
  • 1
  • 1
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
0

Your interfaces don't have any implementations in the methods, and their signatures are identical. Therefore, there is effectively only one version of that method, and it's the implementation in the Test class.

So there isn't actually any name collision.

jefflunt
  • 33,527
  • 7
  • 88
  • 126
0

a) Even though there is collision of method names in interfaces. classes which implement interfaces are expected to implement all the methods of the interfaces. in this case since your class satisfies all the condtions of implementing an interface, it is not producing any error.

the class implemented all the methods of interface Test1 and also Test2 and eventhough method name collides, the coditions to implement satisfies so its not producing an error.

b) there is nothing like inheriting when you implement interfaces, all you need to do is to implement all the methods of interface in your class else it must be declared abstract class.

ylnsagar
  • 524
  • 2
  • 8
  • 16