aspose file tools*
The moose likes Beginning Java and the fly likes Compile time polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compile time polymorphism" Watch "Compile time polymorphism" New topic
Author

Compile time polymorphism

Rajan Venkat
Ranch Hand

Joined: Dec 17, 2006
Posts: 35
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?


regards,<br /> rajan<br /> SCJP 1.4
Rajan Venkat
Ranch Hand

Joined: Dec 17, 2006
Posts: 35
can anyone of you tell me the answer for my question ? thanks.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi Rajan,
Thats perfectly right. You need to *understand* the polymorphism concepts first.

Poly - many, morphism - faces.

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.

I hope this clears your doubt!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Rajan Venkat
Ranch Hand

Joined: Dec 17, 2006
Posts: 35
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 ?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Exactly!

That's why the name is given as such Rajan.
Rajan Venkat
Ranch Hand

Joined: Dec 17, 2006
Posts: 35


Output :

In the Animal version

The above example is for method overloading,would like to know wheather polymorphism applies in this concept.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Yes very much.



The following information proves the same for the method following Polymorphism.

Method Name : doStuff
# of versions : 2
Change Nature : Different Type of Argument [but same number of args.]


HtH.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

That's fine Ilja Preuss . You are right.

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).

HtH.

[ May 21, 2007: Message edited by: Raghavan Muthu ]
[ May 21, 2007: Message edited by: Raghavan Muthu ]
Rajan Venkat
Ranch Hand

Joined: Dec 17, 2006
Posts: 35
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?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi Rajan,


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
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.

Thanks for your reply.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thats great Rajan..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compile time polymorphism