of course here is runtime polymorphism
the code you have written is confusing yourself only
I modified the code
and see the comments in it
and the runtime polymorphism applies to OVERRIDING methods only
and the method with signature (
String a) is not taking part in overriding at all
now you may ask that var-args are chosen at last if the exact method signature is not available
in this case, we have the method with exact signature available
but still the var-args method is chosen due to following reasons
Alpha a = new Alpha(); means that reference type is of class Alphaat comile time, there is only one method availabe in class Alpha i.e. var-args method when the compiler sees the call a.foo("test"), it looks for the exact method signaturebut method public void foo( String a) is not available in class Alphaso at compile time, the method in class Alpha is chosen but as the program runs, due to polymorphism, the overriding method in class Beta is chosen and hence we get the output subclass and not 5 Remember that method polymorphism is only for overrding methods and overrding methods have exactly same signature
hope this helps
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right