// I've cut down the code to get to the point
abstract class TwoDShape {
.... constructors
.... example variables & methods
abstract double area();
}
Below is where it gets confusing, why is TwoDShape shapes[] = new TwoDShape[4]
allowed despite the Abstract rules which should have cause a compile time error ? Why does TwoDShape test = new TwoDShape();
or other similar construction failing to compile, causing errors ? Is it because shapes[]
is an object reference due to it being in an array ? But isn't it an object declaration as well (considering new is also used).
class AbsShapes {
public static void main(String args[]) {
TwoDShape shapes[] = new TwoDShape[4];
shapes[0] = new Triangle("outlined", 8.0, 12.0);
shapes[1] = new Rectangle(10);
shapes[2] = new Rectangle(10, 4);
shapes[3] = new Triangle(7.0);
for(int i = 0; i < shapes.length; i++) {
System.out.println("Object is " + shapes[i].getName());
System.out.println("Area is " + shapes[i].area());
System.out.println();
}
}
}