最初のバージョン:
public interface DeepCopyable<T>
{
T deepCopy();
}
public interface Statement extends DeepCopyable<Statement>
{
}
public interface Expression
{
Expression deepCopy(); // forgot I have an interface for this
}
public class Invocation implements Expression, Statement
{
public final String Field;
public Invocation(String field)
{
Field = field;
}
public Invocation deepCopy()
{
return new Invocation(Field);
}
}
2 番目のバージョン、式インターフェイスの更新:
public interface Expression extends DeepCopyable<Expression>
{
}
しかし、今ではコンパイルエラーが発生します
Error: C:\temp\Invocation.java:1: DeepCopyable cannot be inherited with different arguments: <Expression> and <Statement>
戻り値の型が不変である一般的なケースでは、このエラー メッセージを理解できます。ただし、戻り値の型 A と B で同じインターフェイスを 2 回継承し、さらに実装メソッドが C を返す場合、C は A と B に関して共変です。これは安全ではないでしょうか?