• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple interfaces

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic