Java Generics - implements and extends

后端 未结 2 1407
挽巷
挽巷 2020-12-20 13:31

I am trying to write something like

public class A implements B {}

and

public abstract class M

        
相关标签:
2条回答
  • 2020-12-20 13:46

    When you have interfaces in generics, you still have to use the extends keyword.

    In your case, if you know what T will be :

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

    If you don't and you simply have to implements B with a C type :

    public class A implements B<C> {}
    

    For the second part, once you've defined I you can use it as is in your other generic types :

    public abstract class M<I extends P> extends R<I> implements Q<I> {}
    

    Resources :

    • www.angelikalanger.com - Java Generics FAQs
    • Oracle.com - generics tutorial
    0 讨论(0)
  • 2020-12-20 13:51

    There is no implements keyword in generic bounds. It's only extends

    Furthermore - you should specify the type parameter only in the class definition, and not in supertypes. I.e.

    public class A<T extends C> implements B<T> {}
    
    0 讨论(0)
提交回复
热议问题