• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compile time polymorphism

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rajan Venkat
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone of you tell me the answer for my question ? thanks.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Rajan Venkat
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly!

That's why the name is given as such Rajan.
 
Rajan Venkat
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats great Rajan..
 
reply
    Bookmark Topic Watch Topic
  • New Topic