Why do you expect "bbb bbb"?
Since you are casting the instance to a
, the only information to the compiler on the first call is the version with "aaa"
, so that value is what is used.
In the second version without the cast, the compiler can see the "bbb"
, so that value is what is used.
Polymorphism impacts which method is invoked - but it doesn't impact the parameters passed. Essentially, the default values are supplied by the compiler (at the call-site), so your code is actually equivalent to:
((a)new b()).test("aaa");
new b().test("bbb");
where the "aaa"
and "bbb"
is supplied at compile time, by inspection of the resolved method.