Hello,
Kevin, please
QuoteYourSources for the code snippet above.
Rules are JVM will choose the closest class in the hierarchy. Why do you think that the version with method argument as B will
be called? If you look at the invocations which are
Here c1 is of type C and C extends A, so the closest class in the hierarchy is class A, so the version taking n A reference will be invoked.
Now line 12, we say the type which is passed is D and D extends C and C extends A, so the closest hierarchy again is A, so again the method taking
reference A will be called.
B doesn't come in hierarchy for the invocations because neither C or D extends from B, but rather directly or indirectly from A.
Hope this helps,