0

I have the following classes and interfaces:

public interface Curve<T extends Path<Vector2>> {
    List<T> splines();
}

public final class CachedCurve implements Curve<Path<Vector2>> {
    public CachedCurve(final Curve<Path<Vector2>> source) {
        // ...
    }    
    @Override
    public List<Path<Vector2>> splines() {
       // ...
    }
}

public class BezierCurve implements Curve<Bezier<Vector2>> {
    public BezierCurve(List<Vector2> sample, float tresholdPerpDistance) {
        // ...
    }
    @Override
    public List<Bezier<Vector2>> splines() {
        // ...
    }
}

I use the above like so:

Curve<Path<Vector2>> curve = null;
curve = new BezierCurve(new LinkedList<Vector2>(), 10f);  // Compilation error
curve = new CachedCurve(curve);

I can't seem to fix this compilation error:

Type mismatch: cannot convert from BezierCurve to Curve<Path<Vector2>>

Maybe it's unclear what I'm trying to do from the (apparently incorrect) code. I would like to have one generic interface Curve. Then I want to have one class that can cache any Curve and one that is a special implementation of the Curve with Bezier splines. I wish to code this generically so that I can write:

Curve<Path<Vector2>> curve = null;
curve = new BezierCurve(/* */);
curve = new CachedCurve(curve);

Relevant classes from libGDX:

Janez Kuhar
  • 3,705
  • 4
  • 22
  • 45
  • 1
    Change your class to `class BezierCurve implements Curve>` along with the method signature. – Michael Feb 18 '19 at 12:31
  • Look at this -> public final class CachedCurve implements Curve> { public class BezierCurve implements Curve> { Those classes implements same interface with different generic types, so its not possible to assign BezierCurve to Curve> variable type. You can change generic type of implemented interface in BezierCurve. – Tom Feb 18 '19 at 12:33
  • @Tom But I thought it should work somehow because `Bezier` *extends* `Path`. – Janez Kuhar Feb 18 '19 at 12:39
  • 1
    Like the duplicate shows/explain: That "extends" logic (polymorphism) doesn't work for generics. – Tom Feb 18 '19 at 12:42

0 Answers0