Error returning a generic type interface

I am trying to return an object, which should be an implementation of IClass with a generic type, which is an implementation of IType.

public IClass<IType> createClass()
{
    return new ActualClass();
}

The actual class I want to return extends the class (abstract), with the generic type ActualType:

public class ActualClass extends Class<ActualType>

An abstract class object implements the IClass interface and can be of any type that extends IType

public abstract class Class<T extends IType> implements IClass<T>

ActualType just implements the IType interface

public final class ActualType implements IType

I get the message "Type mismatch error: cannot convert from ActualClass to IClass" when compiling. I cannot understand why, since ActualClass implements IClass (indirectly through a class), and ActualType implements IType.

How can I change this so that it works? What have I done wrong or misunderstood?

IClass IType, , ActualClass, , IType.

+4
2

ActualClass :

IClass<ActualType>
       ^
       |
Class<ActualType>
       ^
       |
   ActualClass

, IClass<ActualType> - ActualClass. IClass<IType>. IType - ActualType, IClass<IType> - IClass<ActualType>, , , - ActualClass.

, IClass<ActualType> IClass<? extends IType>. :

public IClass<? extends IType> createClass() {
    return new ActualClass();
}
+3

createClass :

public IClass<? extends IType> createClass() {
    return new ActualClass();
}

, , , Generics Java , , IClass, IType

+2

All Articles