0
// 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();
  }

 }
}
BradleyPeh
  • 35
  • 7
  • Abstract class cannot have instances, but they can be array because when you creare an array there is no instance. – zhh Jul 28 '18 at 12:15
  • 3
    A TwoDShape is to a TwoDShape[] what an egg is to an egg box. When you create an egg box, you don't create any egg. You just create an empty box that can contain eggs. When you create an array of TwoDShape, you just create that: an array. It doesn't contain any object. – JB Nizet Jul 28 '18 at 12:15
  • Thanks for the clarification, I guess my theory was right after all this thinking – BradleyPeh Jul 28 '18 at 12:20
  • "*I guess my theory was right after all this thinking*" possibly, but based on description from your question it looks like you may be confusing terms like `object` `variable` and `reference`. If that is the case consider reading [What is the difference between a variable, object, and reference?](https://stackoverflow.com/q/32010172). – Pshemo Jul 28 '18 at 12:32

2 Answers2

1

What cannot be created is direct instances of an abstract class:

new TwoDShape(); //this won't compile.

However, this is not creating direct instances of TwoDShape:

new TwoDShape[4]; //this is creating an array of type TwoDShape

The above code creates an array of TwoDShape type. No instance of TwoDShape is created. If you call shapes[0], you will get null, meaning that there's no TwoDShape object created.

In other words, the type of shapes is not TwoDShape, but TwoDShape[]. And array types can be created using new.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
1

This line:

TwoDShape shapes[] = new TwoDShape[4]

creates and empty array of size four capable of storing object of type TwoDShape. The objects themselves are not created - in other words immediately after creation the array holds four nulls.

Now this line:

TwoDShape test = new TwoDShape();

claas the construtor of type TwoDShape to create an object of that type. Since class TwoDShape is declared abstract, the compiler throws an error.

You may want to read those for more clarification:

Konrad Botor
  • 4,765
  • 1
  • 16
  • 26