It is a
Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)[
^] feature.
The above link describes it, provides an example similar to yours, and finally nails it down:
Quote:
The Java virtual machine (JVM) calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable's type. This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language.
In your example the objects in the list are of type
Alpha
,
Beta
, and
Main
. Even while
t
is declared as
Alpha
, the appropriate
doStuff()
method of the "real" object is called.