为什么允许在子类中更改抽象方法的返回类型?
abstract class Animal {
public abstract Animal assing(Animal a);
}
class Lizard extends Animal {
@Override
public Lizard assing(Animal a) {
return new Lizard();
}
}
class Chicken extends Animal {
@Override
public Chicken assing(Animal a) {
return new Chicken();
}
}
而不允许更改参数类型:
abstract class Animal {
public abstract void foo(int x);
}
class Lizard extends Animal {
// compiler error
// the type Lizard must implement the inherited abstract method Animal.foo(int)
public void foo(float x) {
}
}
class Chicken extends Animal {
@Override
public void foo(int x) {
}
}