Enforcing return type for an class that implements an interface

后端 未结 7 1265
青春惊慌失措
青春惊慌失措 2021-01-11 11:51

How do I enforce that the method getFoo() in the implementing class, returns a list of the type of same implementing class.

public interface Bar{
     ....
          


        
7条回答
  •  [愿得一人]
    2021-01-11 12:30

    You should infer the generic on Bar:

    public interface Bar {
      ...
      List getFoo();
    }
    

    You can just parameterize the method, but that will not ensure that the return type matches that of the class.

提交回复
热议问题