'Does Scala call method associated with declared type, or method associated with parent class

Say I have a class Rectangle with a method shapeType that prints "rectangle", and a subclass Square that overrides this method to print "square".

Say I then have a variable of declared type Rectangle, e.g. var shape: Rectangle = ___

I then reassign this variable to be a Square, e.g. shape = new Square (which is allowed as Square < Rectangle)

When I call shape.shapeType, will the compiler choose to print "rectangle", or "square"?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source