通常、サブタイプの値が必要な場所にスーパータイプの値を渡すとエラーになります。
編集:例を挙げてください(非常に奇妙なものですが)
interface Testing{
void printValue(TestClient cl);
}
class TestClient implements Testing{
private String content;
public TestClient(String content){
this.content = content;
}
@Override
public void printValue(TestClient cl) {
// TODO Auto-generated method stub
System.out.println((cl.content!=null ?cl.content :"Hello World"));
}
}
public class Test{
public static void main(String args[]) {
Testing t = new TestClient("Hi Friends");
Testing t1 = new TestClient("Hi Enemies");
t1.printValue(t); // generates error (The method printValue(TestClient) in the type Testing is not applicable for the arguments (Testing)
}
}
ここで、t1.printValue(t)
メソッドはエラーを生成しますThe method printValue(TestClient) in the type Testing is not applicable for the arguments (Testing)
ただし、 Generics では、すべてのパラメーター化された型は対応する生の型のサブタイプであるため、生の型が期待される場所にパラメーター化された型の値を渡すことができます。
では、なぜ Java は、パラメーター化された型が期待される場所に未加工の型の値を渡すことを許可するのですか? ただし、未チェックの変換警告を生成することで、この状況にフラグを立てます。