• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HOW TO IMPLEMENT TWO SAME METHODS FROM DIFFERENT INTERFACES IN ONE CLASS

 
Ravinder Gonalla
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i have two same methods in two different interfaces.
if one class implemnting two interfaces, then how should
i implement two same methods in that calss.

Thanks ,
ravinder.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ravinder Gonalla:
hi,
i have two same methods in two different interfaces.
if one class implemnting two interfaces, then how should
i implement two same methods in that calss.

Thanks ,
ravinder.


Hi Ravinder,

Welcome to Javaranch.
Please choose the forum carefully.This forum is for Other Java products and servers.

If you say interfaces then nothing is implemented in there.So what is the issue , the class implementing the two interfaces would have only one method of the same signature of what is present in the two interfaces.Only one implementation.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This only works if the methods in the interfaces are compatible.

If the method in the first interface should do A, and the method in the second interface should do B, and A and B are opposite actions, then you will break the contract on one of the two.

Example:

Of course, if this is the case, then there is a case of bad design.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got asked this question in an interview a long time ago. I scratched my head for quite a while, thinking there was some special piece of magic for dealing with this issue. But there isn't.

If you have two interfaces with a method with the same name and signature, and you choose to declare that a particular class implements both interfaces, then you just write one single method in the class. You cannot tell which interface's version is being called. Therefore, if the name-named methods in the two interfaces have different purposes, you are stuck.

The answer that they wanted in this interview was that you should refactor the class, so that it no longer directly implements the interfaces, but instead has methods that return an object implementing those interfaces. That's a good solution, if such a refactoring is possible within the context of your overall application.

When one first comes across it, the issue of same-named methods in interfaces sounds as if it would be a dreadful limitation of Java. In practice, I have been doing Java for nearly 10 years, and have never come across a real instance of it.

By the way, Ravinder, DON'T SHOUT. Please read HowToAskQuestionsOnJavaRanch.
[ September 26, 2007: Message edited by: Peter Chase ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic