carlo
carlo

Reputation: 386

Java Generics Bound Mismatch Compilation Error

I have an interface declared in this way:

public interface GenericBo<T, PK extends Serializable> {

 public ResultObject create(T newInstance);
...
}

and an abstract class declared in this way:

public abstract class GenericServiceImpl<T, PK> implements GenericService<T, PK> {

    private final GenericBo<T, PK> bo;

    ......
    ......
    }

but I have an error in the abstract class while declaring the 'bo' instance: Bound mismatch: The type PK is not a valid substitute for the bounded parameter of the type GenericBo

How should be declared the "GenericBo bo" instance? What is the correct code?

Upvotes: 1

Views: 273

Answers (2)

Paul Draper
Paul Draper

Reputation: 83235

You have

class GenericBo<T, PK extends Serializable>

so when you declare

private final GenericBo<T, PK> bo;

PK must extends Serializable.

But

class GenericServiceImpl<T, PK>

has no such restriction. You need to add it.

public abstract class GenericServiceImpl<T, PK extends Serializable> implements GenericService<T, PK> {

    private final GenericBo<T, PK> bo;

}

Upvotes: 2

ethanfar
ethanfar

Reputation: 3778

The problem is that in the abstract class, the PK is not required to extend Serializable. Either change the abstract class generic definition so that PK extends Serializable, or remove that requirement from the interface.

Upvotes: 1

Related Questions