Reputation: 2205
I have an interface with prototype
public interface genericInterface <E extends Comparable <E>>{}
and now I want to implement this inerface in my class, what I should wirte in class definition? The following line is giving me an error (I have implemented all the methods in the interface)
public class MyClass<Integer> implements genericInterface**<Integer Comparable<Integer>>**{ }
Syntax...
genericInterface<Integer Comparable<Integer>> -- error
genericInterface<Integer> -- error
what I should write in place of <Integer Comparable<Integer>>
to make it compatible?
Upvotes: 1
Views: 116
Reputation: 16354
The class declaration should just be:
class MyClass implements genericInterface<Integer>
Generic parameters after the class name are for parameters of that class, not of classes that it is implementing.
So, for example:
interface genericInterface<E extends Comparable <E>> {
E getKey();
}
class MyClass<T> implements genericInterface<Integer> {
T getValue();
}
MyClass<String> m = new MyClass<String>();
Integer a = m.getKey(); // Because MyClass always has E = Integer
String b = m.getValue(); // Because m has T = String
genericInterface<Integer> g = new MyClass<Boolean>();
Integer c = g.getKey();
You can also do:
class MyClass<E extends Comparable <E>> implements genericInterface<E>
Then you can have instances of MyClass
parameterized with any type that would work for genericInterface
.
Upvotes: 9
Reputation: 3486
Generics are available in Java only since version 1.5. You should use raw types.
Upvotes: 0