File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Interface conflict Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface conflict" Watch "Interface conflict" New topic

Interface conflict

Cecil Phillip
Ranch Hand

Joined: Nov 05, 2001
Posts: 40
Lets say that you implement 2 interfaces into a class, TestClass. Now the first interface declares 2 methods read() and write(), and the second implements load() and write(). In you class definition, how are the write() in both interfaces implemented to avoid conflict and confusion
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
I think both implementations will be satisfied, but if you want the calls to write() to do different things, you are going to have to have some kind of code within write() to determine what it should do... usually you won't have this problem tho. Usually similar calls will have different variables passed like write(String s) and write().
If you are designing your own interfaces, just name them something different like jobWrite() and nameWrite().
Is there a specific example you have of this or is it a hypothetical question?
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
If the method signatures for the two write() methods are different, there is no problem at all since they are completely different API calls.

If the signatures are the same, and the return type is the same, then you have basically declared the same API call in both interfaces. You might want to rethink if this is what you really want; if it is what you want, why not create a super-interface and have your two interfaces extend this, and move the write() method into the super-interface.

Finally, if your signatures are the same, but the return types are different, you'll get a compiler error since the ambiguity cannot be resolved.
[ June 06, 2002: Message edited by: Rob Ross ]

SCJP 1.4
Cecil Phillip
Ranch Hand

Joined: Nov 05, 2001
Posts: 40
I think I kinda got it now. Thanks
I agree. Here's the link:
subject: Interface conflict
It's not a secret anymore!