Neelam
Neelam

Reputation: 370

Implementing a Generic Interface In Java

I am new to Java Generics. I have to implement an interface which is of 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 Lets say my Class is C

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

The java compiler is not letting me use it this way. Also I do not want to use raw types. Please help.

Upvotes: 4

Views: 1427

Answers (2)

davioooh
davioooh

Reputation: 24706

If your implementing class is still a generic type you have to use this syntax:

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

As explained by Eran.

If C is not generic you simply need to specify the type argument for your interface:

public class C implements B<TypeExtendingA> {}

Where TypeExtendingA implements or extends A (or is A)

Upvotes: 3

Eran
Eran

Reputation: 394156

It should be

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

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

Upvotes: 8

Related Questions