Suppose I have an interface as follows:
public interface Foo<T> {
T doSomething();
}
Now, are both the following allowed?
public class Bar implements Foo<Number> { ... }
public class Bar2 extends Bar implements Foo<Integer> { ... }
On one hand, I seem to not think so, since Bar2
"implements Foo
twice", even though Integer
is a subclass of Number
. On the other hand, wouldn't this be a case of covariant return type on doSomething()
? Or is the compiler not smart enough to detect as such?
This is not an interface, it seems to be a class(or abstract class which is also missing the keyword
abstract
), it should be like:-Besides
Will give you compile time error ,
The interface Foo cannot be implemented more than once with different arguments: Foo<Number> and Foo<Integer>
But if you instead do :-
This will not give an compile time error, and if you implement
doSomething()
in Bar2 it will take that into consideration when you do :-or else it will run the
doSomething()
fromBar
and obviously if you do:-
it will take into consideration
doSomething()
ofBar
, since it does have only one implementeddoSomething()
into account this time , i.e ofBar
(which you have to implement sinceBar
is implementing the interfaceFoo
:) )