Compile time polymorphism

I see some of them saying method overloading is the compile time polymorphism,but i disagree with that the polymorphism and inheritance are nothing to do with method overloading.we know very well that method overriding is the runtime polymorphism then What is the compile time polymorhism?

The ability to have one more than one version of the executable piece of code in the same name to invoke is called POLYMORPHISM.

The ability to take a decision and thereby creating the binding is what gives you the name.

The Compile time polymorphism comes in such a way that the compiler is able to differentiate the exact version of code to invoke during the compile time itself. Rather, the compiler *can take a decision of which exact method to invoke* during the compile time itself.

Whereas in Runtime Polymorphism, the compiler is unable to take such a decision during compilation. Moreover, compiler-is-left-free-for-that-sake until runtime with which it can decide and invoke the appropriate version of the method.

Only when Inheritance is in picture, you can very well play around with the Polymorphism as you are given the rights to assign the child class objects to any of its parent/super class references.

The ability to take a decision and thereby creating the binding is what gives you the name.

The Compile time polymorphism comes in such a way that the compiler is able to differentiate the exact version of code to invoke during the compile time itself. Rather, the compiler *can take a decision of which exact method to invoke* during the compile time itself.

So you meant to say that the compiler will decide which version of code to run at compile time - Method Overloading ?

This can be a little bit confusing, because most people actually mean *runtime polymorphism* when they talk about "polymorphism".

Method overloading is a form of compile time polymorphism in Java. The template mechanism of C++ is another example.

Note that inheritance is not necessary for polymorphism.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

You can have the polymorphism in the same class itself. No need to have only Inheritance.

I meant to say polymorphism (especially runtime polymorphism) can very well be achieved with Inheritance where you can play around with parent-sub class objects!

This can be a little bit confusing, because most people actually mean *runtime polymorphism* when they talk about "polymorphism".

I don't think this statement makes sense because Polymorphism is a general term and it always suits to both the types (Compile-time and Runtime). Not necessarily to be meant only/mostly for either of the types.

The Compile-time and run-time polymorphism are also known/called as Early Binding/Late Binding(as what i had described in my earlier reply).

From my earlier reply and the response from others , i can say that the polymorphism doesnt get apply to the method oveloading with inheritance .

In Headfirst java book , i read a statement that the polymorhism and inheritance are nothing to do with overloaded methods. What does it mean?

I think you are not clear! Overloading is one kind of polymorphism and it DOES DEFINITELY deal with it!!

As we had discussed, Inheritance helps achieving polymorphism but ITS NOT THE ONLY WAY TO DO it!! Even you can have the Polymorphism in the Single Class itself!

Probably you can go through the HF Java book again what they actually meant to convey with respect to the particular scenario they have taken to explain. I am sure they would not have conveyed a wrong message!

I think it might have been for the combination of *polymorphism and inheritance* are nothing to do. If you want to have an overloaded method, its not necessary that you should have an inheritance in place!! - Does it make some sense?

Rajan Venkat
Ranch Hand

Joined: Dec 17, 2006
Posts: 35

posted May 21, 2007 22:54:00

0

Hi Raghavan,

I understand the compile type polymorphism .

Compiler finds correct version of code to be invoked at the compile time based upon the arguments - Method Overloading

Method invocation happened at the runtime based upon the object type - Method overriding (Run time polymorphism)

Was clear with the above concept previously but i got confused when i checked the method overloading with inheritance.