This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

implementing 2 interfaces.  RSS feed

 
manoj rajkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
A very simple doubt:
If I have 2 interfaces, A,B and I need to implement both of them but having the following method signature:

interface A{ public int someMethod(); }
interface B{ public float someMethod(); }
class C implements A,B

How do I implement?
Does such a situation arise in real world (I mean computer world) scenarios?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only an interface can implement (extend to) multiple interfaces.

If you want to implement multiple interfaces in an ordinary class, you'd better to review your design. You can implement only one interface and extend multiple abstract classes.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummm, Bauke, you sure about that?

Of course a class can implement any number of interfaces, as Manoj knows. Unfortunately, they must all be compatible interfaces, meaning that if two interfaces have a method with the same name and argument list, they must also have the same return type and compatible exception specifications. In the present case, where the two methods have different return types, I'm afraid you're out of luck; one Java class can not implement both of these interfaces.

I've never had this come up in real life, but Gosling and Arnold do mention it in "The Java Programming Language."

Instead of having one class implement both interfaces, you might have the class implement only one, and then provide a method like "getImplementationOfIntefaceB()" which returns another object which implements B by calling methods of A provided for this purpose -- that second object could be an inner class.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke is indeed wrong. A class can implement more than one interface.
I think he may be confused with extension. A class can extend only one other class, while an interface can extend several interfaces.

As to conflicts like you mention, I've never encountered them in nearly a decade in object oriented programming (most of it using Java) or if I have they were easily avoided by small design changes.
Doesn't mean they don't happen, but they're rare enough that you don't need to worry about it.
[ October 27, 2006: Message edited by: Jeroen T Wenting ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

perhaps such problems may arise when people like me won't stop calling all our methods "foo" constantly.

Here's an example with two incompatible interfaces. Class A will not compile, even if it doesn't try to implement the methods (as A is abstract).





Yours,
Bu.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive me for my confusion. I probably had a lack of some coffee.
 
manoj rajkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
thanks for the clarifications. Just to summarize,

1>The conflict is very rare and if it does happen, a slight change in design(say changing the name of one of the interface methods) would solve the problem as mentioned by our vastly experienced Jeroen .

2> Sometimes, we may not be able to make changes to the interface, then we can solve the issue in this way:

We have a class A implement Inter1 and an inner class innerA(or we can use aggregation) implementing Inter2, and have methods foo and foo2(which internally calls the foo implementation of InterfB). It seems like a rarely used design pattern we call it Ernest's Design pattern(inside the scope of this discussion).



class A implements Inter1{

void foo() {}

byte foo2(){
return inner.foo();
}

Inner inner = new Inner();

class Inner implements Inter2{
byte foo(){
return (byte) 10;
}
}

}

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by manoj rajkumar:

We have a class A implement Inter1 and an inner class innerA(or we can use aggregation) implementing Inter2, and have methods foo and foo2(which internally calls the foo implementation of InterfB).


Although methods like foo2() might be useful, I was actually saying that A should have a method getAsInter2() which returns the innerA object. The innerA object is a "view" of A which implements Inter2.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bauke Scholtz:
...I probably had a lack of some coffee.

For robust Java, programmers should always implement more than one cup of coffee.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16027
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to implement myself some Robusta coffee right now...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!