I am creating following class hierarchy:
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());
}
}
What I wish to do is to hide the function getVolume()
for TwoDimentionalShape
class, as it will be used for ThreeDimentionalShape
class.
The problem is that I have declared the function as protected, but when I call it from main()
, the program is working. Why is this happening?