• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple interfaces

 
Shanti Ramchandran
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic