4

The problem:

base interface:

 IBase 

descendants:

InterfaceA extends IBase<Interface1> InterfaceB extends IBase<Interface2> 

When I try:

InterfaceC extends InterfaceA, InterfaceB 

I receive compile error:

The interface IBase cannot be implemented more than once with different arguments 

Does exist workaround? Thanks.

1
  • I have check - but solution is cluster for classes, I don't know how make it for interfaces. Commented May 4, 2012 at 10:24

2 Answers 2

4

This is not possible and it cannot be in Java, at least. Think of the following scenario:

 interface Base<K> { K get(); } interface A extends Base<String> { String get(); } interface B extends Base<Integer> { Integer get(); } interface AB extends A, B { ?? } 

When you go try to implement AB, if it were possible, what type would the get() method return. In Java, two methods in a class cannot have the same name/args but different return types.... hence, this is forbidden.

If you actually need some functionality similar to what you would get should Java allow this, I would suggest the following:

 abstract class C { A a; B b; String aGet() { return a.get(); } Integer bGet() { return b.get(); } } 

Or, to keep generics:

abstract class C<K, T> { Base<K> a; Base<T> b; K getA() { return a.get(); } T getB() { return b.get(); } } 
Sign up to request clarification or add additional context in comments.

Comments

3

Generics are basically compile time check only, so InterfaceA and InterfaceB are the same.

A general workaround for what you are suggesting is hard to come up with, you would probably have to specify your exact situation more. But do you really need THAT class to implement the two, why not two different classes? Maybe nested classes, or even anonymous inner class?

3 Comments

yes, I really need that class should be used as InterfaceA and/or InterfaceB
You could do it without specifying the generic type and implement it once with Object and do it the old fashioned why with casting (and ignore warnings)
Try and do it by implementing IBase<Object> then instead perhaps?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.