Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interfaces extending multiple Interfaces???

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
I know Java doesn't support Multiple Inheritance, so this is really confusing me.

I created an Interface, and I wanted that interface to implement two other interfaces (i.e. force all classes that implement my interface to also implement the other two interfaces). When I tried doing this I got an error:
Code:
public interface BUser	implements Comparable<BUser>, Comparator<BUser> { ... }
Eclipse gave me an error there saying "Syntax error on token 'implements', extends expected".

So I changed it to this and it got rid of the error:
Code:
public interface BUser	extends Comparable<BUser>, Comparator<BUser> { ... }

1 - I thought interfaces couldn't 'extend' anything?
2 - Why can't I 'implement' 2 interfaces here?
3 - If I keep on coding and just leave it as 'extends' instead of 'implements', can anyone see any problems down the road?
 
An interface can't 'implement' anything ... it is an interface not a class. A class can contain code and therefore represents an implementation, an interface does not. So the 'implements' keyword is out. One interface can extend another. It is merely adding to the method definitions of the interface it extends.

Tim
 
Why don't you get the class to implement both interfaces?

Cheers,
Dian
 
I always thought 'implements' was used to derive from an Interface while 'extends' was used when deriving from a class, but I guess it makes sense since the word 'implement' implies that some code will be written...

Diancecht said:
Why don't you get the class to implement both interfaces?
Because I want to pass references to the interface to functions and have them be able to call the compare functions from the other two interfaces. Plus that will force all derived classes to implement those interfaces at compile-time instead of waiting until run-time to find out they forgot to implement them.


Can you see any problems with extending the interfaces like I've done?
One thing I noticed is that the class that implements my BUser interface has to override the functions as compareTo( BUser ) instead of compareTo( BUserBase ) Could that cause problems for me since I'm passing BUser references to functions...?
 
I'm not sure I'm getting the point, but if a class A implements interfaces B and C, objects from class A can be passed as both B and C.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top