Campbell Ritchie wrote:Although you sometimes hear terms like, “compile‑time polymorphism,” I do not believe that overridingoverloading constitutes polymorphism at all. It means that the behaviour of two objects at runtime might be different because their runtime type is different even if their compile‑time types (=declared types) are the same.
In Java® that means overridden accessible (not private) non‑final instance methods. And nothing else.
Campbell,
i think you are misinterpreting the 2 different things.the reason this is less intuitive to you can be you are thinking about the polymorphism in regard of runtime polymorphism only.though i would agree with you that their is not any term like compile time polymorphism(oficially,in java,however i am not sure about other languages).but i will not agree with you for your views on polymorphism of the overloaded methods(called as
Ad hoc polymorphism in computer science,but again we don't have this term in java).this type of polymorphism can be applied to functions(called as methods in java and luckily we have the implementation of this polymorphism in java).
Let's see an example:
If you would see the line 35 and 36,compiler is playing a role in polymorphism(ad-hoc and generic) here.compiler knows that the type of an object is Polymorphism here so it would place the call at line 35 as the call to the method having signature print(String,int) in class Polymorphism[yes the compiler will put the call for the method present in class Polymorphism] but along with the effect of run-time polymorphism(also called as
Subtyping polymorphism) during run-time,the method actually invoked would be the one overriden(present in PolymorphismExtended).means the call at line 35 is due to the effect of both the overloading(ad-hoc polymorphism) and the runtime polymorphism and because it was the compiler who finds the correct method(amongst the overloaded) it was not a bad idea to call this type of polymorphism as compile-time polymorphism.
now come at line 36,here again compiler has a very great role and their is a third kind of polymorphism is actually going on here called as Generics in java(but generally it is also called
Parametric polymorphism).here it was compiler who will infer the T as Integer at line 36 along with finding the proper method(accordingly with the signature-ad hoc polymorphism).and in fact at this line we have all the 3 types of polymorphism in action:
→compiler is playing a role in ad hoc polymorphism and parametric polymorphism,it will detect the proper call and infer the parameter T.
→jvm will play a role in run-time polymorphism(by detecting the real type of object at run-time) by calling the overriden version(present in class PolymorphismExtended) of the generic method.
again in my opinion it is not a bad idea to call a parametric polymorphism(generics) as compile-time polymorphism(in java) as compiler is only playing role for it.
for a proof purpose you can see the disassambled code for the main method,here it is:
look at the line 10(marked as 12 inside bytecode),there are 2 things invokevirtual which means the virtual method invocation(the runtime polymorphism) and the next thing is the method written as
"Method Polymorphism.print:(Ljava/lang/String;I)V" which shows compiler opts the method with signature print(String,int) having void as a return type and i can read the line as
"virtual method invocation of the method present in class Polymorphism with signature print(String,int)V."V is for void.so it's easy to say here we have the combined effect of the ad hoc and the runtime polymorphism.
now come at line 24 and 27.you can easily see there,compiler is inferring the T as Integer and then at next line we have again the effect of combined poymorphism(of the 2 mentioned above) in action.
and @Junilu thanks for the correction for my previous post regarding signature.i am glad that we have members like you for the proper guidance where ever we are wrong.
Kind regards,
Praveen.