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.
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.
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.