Note A.3.4.
Even though you cannot directly instantiate a
Shape, you can use polymorphism to create, for example, a MutableList of Shape and assign concrete (non-abstract) objects such as Circle and Rectangle to its elements, as in Listing A.3.5.
fun main() {
val shapeList: List<Shape> = listOf(
Circle(0.0, 0.0, 1.0),
Rectangle(1.0, 1.0, 2.0, 3.0)
)
for (s in shapeList) {
println(String.format("%s area: %.2f perimeter: %.2f%n",
s, s.calcArea(), s.calcPerimeter()))
}
}
