posted 17 years ago
also, think of it this way... the line
Circle myCircle = new Circle();
creates two things. it creates a Circle object, and it creates a REFERENCE that points to a Circle. in this case, that reference is named myCircle.
in Rusty's example:
Shape circle = new Circle()
you are here creating a Circle object, and a REFERENCE variable, named circle, that refers to a Shape. since a Circle IS-A Shape, everything is ok.
your array is NOT an array of Shapes, but an array of REFERENCES to a Shape. it doesn't matter what the final type of that object is, as long as it is a Shape of some kind.
the beauty here is that later, if somebody makes a Hexagon, Trapazoid or a Parallelagram class that extends your Shape class, you don't have to change your code around the array.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors