File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes HOW TO IMPLEMENT TWO SAME METHODS FROM DIFFERENT INTERFACES IN ONE CLASS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HOW TO IMPLEMENT TWO SAME METHODS FROM DIFFERENT INTERFACES IN ONE CLASS" Watch "HOW TO IMPLEMENT TWO SAME METHODS FROM DIFFERENT INTERFACES IN ONE CLASS" New topic
Author

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

Ravinder Gonalla
Greenhorn

Joined: Sep 21, 2007
Posts: 1
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

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HOW TO IMPLEMENT TWO SAME METHODS FROM DIFFERENT INTERFACES IN ONE CLASS