int x=22;
long z=24;
//now in this case int is smaller than long so
z=x; // is quite appropriate as it implicitly converts from int to long(widening)
同様に、次のようなクラスがあります。
private static class Box {
private int width;
private int height;
private int length;
//...
}
private static class WeightBox extends Box {
private int weight;
//...
}
public class Main {
public static void main(String args[]) {
Box simpleBox = new Box();
WeightBox wt = new WeightBox();
simpleBox = wt; //we can always do this
//wt = simpleBox cannot be done implicitly
//for this to work we have to use type casts
}
}
simpleBox = wt
simpleBoxが基本クラスに属し、wtが拡張クラスに属しているのに、なぜそれができるのでしょうか。拡張クラスは基本クラスよりも大きくすべきではありませんか?