Implementing a Generic Interface in Java

I am new to Java Generics. I have to implement an interface that has a generic type. The syntax is as follows:

public interface A{}

public interface B<T extends A>{
    public T methodB(T a) ;
}

Now I have to implement B so that my class C is

public class C implements B<T extends A>{}

The java compiler does not allow me to use it that way. Also I do not want to use raw types. Please, help.

+4
source share
2 answers

It should be

public class C<T extends A> implements B<T>

The type parameter is declared after the class name, and can later be used in the implements clause.

+7
source

If your implementation class is still a generic type, you should use this syntax:

public class C<T extends A> implements B<T> {}

As explained by Eran .

C , :

public class C implements B<TypeExtendingA> {}

TypeExtendingA A ( A)

+3

All Articles