aspose file tools*
The moose likes Beginning Java and the fly likes Multiple  interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiple  interfaces" Watch "Multiple  interfaces" New topic
Author

Multiple interfaces

Shanti Ramchandran
Greenhorn

Joined: Nov 21, 2000
Posts: 2
Hi
Here's a piece of code:
class MultipleInterfaces
{
public static void main(String args[])
{
Imp i = new Imp();
}
}

interface i1
{
void method();
}
interface i2
{
int method();
}

class Imp implements i1, i2 // 2 INTERFACES WITH METHODS THAT DIFFER ONLY BY RETURN TYPE
{
public void method()
{
System.out.println("Implementing i1");
return;
}
public int method()
{
System.out.println("Implementing i2");
return 1 ;
}
}
Here's my problem.
If I had 2 interfaces containing methods with the same name and parameter list but differing in their return types and a class that wanted to implement both of them, whether there was a way out to the problem.
Thanks
Shanti
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Shanti,
if you have two methods in two different interfaces with the same name and parameter list but different return types, you will not be able to call them both after implementing both interfaces. You will have to modify the parameter list and/or return type for one of the interface methods. If they share a param list and a return type, they can both be implemented with one overriding method in your class. If they differ in their param lists, you will end up with overloaded methods in your class. There is no solution (that I am aware of) for different return types and the same param list.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple interfaces
 
Similar Threads
Can a class implement 2 different interfaces that have the same identical method?
Interfaces
Broader exceptions thrown by methods that implement interfaces
how to implement two interfaces having same methods
why interface variables are static and final. what is reason to declare them as like that?