次のクラス階層を作成しています。
abstract class Shape{
protected abstract float getArea();
protected abstract float getVolume();
}
abstract class TwoDimentionalShape extends Shape{
public abstract float getArea();
protected float getVolume(){
return 0;
}
}
class Square extends TwoDimentionalShape {
float width, height;
Square(float w, float h){
width = w;
height = h;
}
public float getArea(){
return width*height;
}
}
public class ShapeTest {
public static void main(String args[]){
Shape s = new Square(3, 4);
System.out.println(s.getVolume());
}
}
私がやりたいのは、クラスで使用されるので、クラスの関数getVolume()
を非表示にすることです。TwoDimentionalShape
ThreeDimentionalShape
問題は、関数を保護されていると宣言したのに、から呼び出すとmain()
、プログラムが機能していることです。なぜこうなった?