Not able to create generic bounded class objects with interface

I am trying to use bounded types with generics to create generic objects of subclasses (these implement an interface). But I am getting type mismatch errors when initializing objects with the subclasses.

Here is the interface:

public interface ScannableEntity {

Here's the class that implements this interface:

public final class Attachment implements ScannableEntity {

Now I created 2 classes (SegmentPageScanResult and ItemProcessor) with the bounded generic type as:

public class SegmentPageScanResult<TEntity extends ScannableEntity> {


public class ItemProcessor<TEntity extends ScannableEntity> {

    void submitAndExecute(SegmentPageScanResult<TEntity> pageScanResult) {
          . . .

When I am trying to initialize the SegmentPageScanResult and try calling submitAndExecute method of ItemProcessor from a Unit test as follows:

public class ScanTest {

    private ItemProcessor<Attachment> itemProcessor;

    public void testDoScan() {
        Attachment mockRecord = new Attachment();
        SegmentPageScanResult<Attachment> segmentPageScanResult = SegmentPageScanResult.builder()


I get the error -

Required type: SegmentPageScanResult<Attachment>

Can someone please help me understand why I am not able to initialize the generic object with the class implementing the interface?


  • I think you might have done:

    ItemProcessor<Attachment> itemProcessor = new ItemProcessor<>();

    And you also have:

    SegmentPageScanResult<ScannableEntity> segmentPageScanResult = ...

    So when you call:

    itemProcessor.submitAndExecute(segmentPageScanResult, TEST_SEGMENT_ID, TEST_SCAN_ID);

    There is a mismatch between the type of the itemProcessor (Attachment) and SegmentPageScanResult (ScannableEntity). So you probably need to create the ItemProcessor and the SegmentPageScanResult with the same type parameter.

    EDIT: It's not completely clear what you are trying to achieve but maybe this can help:

    public class ItemProcessor<T extends ScannableEntity> {
        private List<T> items;
        void submitAndExecute(SegmentPageScanResult pageScanResult) {
    public class SegmentPageScanResult {
        private final List<ScannableEntity> items = new ArrayList<>();
        public void setScannedItems(List<? extends ScannableEntity> items) {

    So the SegmentPageScanResult no longer has a type parameter because it only handles ScannableEntity instances. To allow setting different types from each ItemProcessor, the method parameter allows subtypes with List<? extends ScannableEntity>