Generics: Inheriting from an abstract class that implements an interface

前端 未结 4 1334
臣服心动
臣服心动 2020-12-29 05:31

I have the following interface:

public interface SingleRecordInterface {
    public void insert(T object);
}

I have the abstract c

4条回答
  •  自闭症患者
    2020-12-29 05:59

    The problem is in your declaration of

    public abstract class AbstractEntry implements SingleRecordInterface {}
    

    This is the place where you define what is type argument (AbstracEntryBean) for the type parameter T.

    Therefore, T is AbstracEntryBean, and when you intend to override this method to finally implement it you are required to provide the exact method signature for the method. In this case:

    @Override
    public void insert(AbstractEntryBean object) {
        // TODO Auto-generated method stub
    }
    

    Since Java requires the exact same method signature to override a given method.

    You can either provide a type parameter for your class, as others have suggested, or provide a bridge (overloading) method as follows:

    //overloading
    public void insert(SpecificBean object){
      insert((AbstractEntryBean) object);
    }
    

提交回复
热议问题