aspose file tools
The moose likes Beginning Java and the fly likes implementing 2 interfaces. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "implementing 2 interfaces." Watch "implementing 2 interfaces." New topic
Author

implementing 2 interfaces.

manoj rajkumar
Greenhorn

Joined: Sep 11, 2006
Posts: 22
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

Joined: Oct 08, 2006
Posts: 2458
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.


Code depot of a Java EE / JSF developer | JSF / Eclipse / Tomcat kickoff tutorial | DAO kickoff tutorial | I ♥ Unicode
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

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.


[Jess in Action][AskingGoodQuestions]
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
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 ]

42
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Forgive me for my confusion. I probably had a lack of some coffee.
manoj rajkumar
Greenhorn

Joined: Sep 11, 2006
Posts: 22
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
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12954
    
    3

I'm going to implement myself some Robusta coffee right now...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: implementing 2 interfaces.
 
Similar Threads
Interface containing an abstract method.
class implementing interface
need your help about a access qeustion.
some questions on Interfaces
Interface