1

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?

3
  • You can't use generics pre java 1.5. Commented Feb 11, 2014 at 15:05
  • I am using java 1.7... Commented Feb 11, 2014 at 15:06
  • 1
    You should change your Java 1.4 tag then. Commented Feb 11, 2014 at 15:07

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks a lot but then if I have a method in the interface method(Myclass<E> arg1) would give me an error right? saying that myclass is not generic , it should be parametrized with <E>
0

Generics are available in Java only since version 1.5. You should use raw types.

2 Comments

He said he is using java 1.7.So there should be no reason why he should not be using powerful features of geenrics
I left answer before he left comment about 1.7. Also, previously question had tag java 1.4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.