wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Compile-time & Runtime polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compile-time & Runtime polymorphism" Watch "Compile-time & Runtime polymorphism" New topic
Author

Compile-time & Runtime polymorphism

Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
What are the differences between Compile-time polymorphism and runtime polymorphism? Pls explain with some simple examples. Which one is better in performance-wise? Also pros and cons of the both.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Method overloading is often referred to as compile polymorphism, whereas method overriding is runtime polymorphism. The difference is in when the actual code to execute is resolved - at compile time or runtime.

Comparing them against each other doesn't make much sense, though, as they solve totally different problems.


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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Ilja's response seems right on the money when it comes to Java. In C++, templates also provide a type of compile-time polymorphism since the compiler decides which function to call at compile-time. Although the new Generics in Java are similar to templates in a lot of ways, I think this is one place where they differ. From what I understand about Generics, the exact code to run is still decided at runtime, so this might also be considered another form of runtime polymorphism.

Layne


Java API Documentation
The Java Tutorial
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Layne Lund:
From what I understand about Generics, the exact code to run is still decided at runtime, so this might also be considered another form of runtime polymorphism.


I don't think generics in Java provide any polymorphism at all. The compiler generates exactly the same byte code as you would do manually without generics. The generics syntax is "just" syntactic sugar so that the compiler can generate the casts for you and do some advanced static type checking.
Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
Thanks for your reply. If I want to learn more about Compile-time & runtime polymorphism from the ground-up i.e.,the concept should be explained in such a way the layman could understand, which material or site you will suggest.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Originally posted by Ilja Preuss:
Method overloading is often referred to as compile polymorphism, whereas method overriding is runtime polymorphism. The difference is in when the actual code to execute is resolved - at compile time or runtime.

Comparing them against each other doesn't make much sense, though, as they solve totally different problems.


I'm certainly not saying you're wrong, just that I dont' get it.

As I understand it, polymorphism means being able to use a subclass when an instance of the base class is called for. Method overriding is one of the pieces of that. However, method overloading is just syntactic sugar and has nothing to do with polymorphism or any other OO concept at all. To back that up, I'll point out that you can use the C preprocessor to achieve what looks like method overloading in that non-OO language.

Or is this just an "Alice in Wonderland" case of using a word to mean "just what I choose it to mean -- neither more nor less?"

Ryan
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ryan McGuire:
As I understand it, polymorphism means being able to use a subclass when an instance of the base class is called for.


That's most often what we OO developers mean when we talk about the polymorphism. (Actually the whole notion of binding runtime polymorphism to subclassing is very specific to manifestly typed languages like Java. It's one of the reasons Smalltalkers look down on us... )

The actual definition is much more general, though. See http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 or http://searchsmb.techtarget.com/sDefinition/0,290660,sid44_gci212803,00.html for example.
 
Consider Paul's rocket mass heater.
 
subject: Compile-time & Runtime polymorphism
 
Similar Threads
Method Overloading and overriding
Polymorphism
PolyMorphisn
polymorphism
Polymorphism: to overriding, not to overloading