Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface conflict

 
Cecil Phillip
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Cecil Phillip
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I kinda got it now. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic