Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes run-time and compile-time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "run-time and compile-time" Watch "run-time and compile-time" New topic
Author

run-time and compile-time

John Paterson
Ranch Hand

Joined: Mar 12, 2012
Posts: 125
Hi folks...

Do have a look at the following code:



Is it true that at comple time it's checked if the 'eat' method exists in the Animal class and at runtime Java selects the actual version of the method that is to be run, which in this case for object ref b it would have to be the eat method of the 'Horse' class??

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

John Paterson wrote:Hi folks...

Do have a look at the following code:



Is it true that at comple time it's checked if the 'eat' method exists in the Animal class and at runtime Java selects the actual version of the method that is to be run, which in this case for object ref b it would have to be the eat method of the 'Horse' class??



yes you are right John.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

This is known as compile time and run time polymorphism.


Tell the difficulties that i am difficult.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

saloni jhanwar wrote:This is known as compile time and run time polymorphism.

polymorphism always at runtime. there is no such thing as compile time polymorphism in java.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Seetharaman Venkatasamy wrote:
saloni jhanwar wrote:This is known as compile time and run time polymorphism.

polymorphism always at runtime. there is no such thing as compile time polymorphism in java.


In context of method overloading we can say this as compile time polymorphism and i don't think it is wrong.what do you think ?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

saloni jhanwar wrote:what do you think ?

well. polymorphism- apply same method on different objects and behavior changes depends on objects(overriding).
<edit> over loading, does not comply this<edit>

I know, still some programmer say overloading as compile time polymorphism. I dont agree with them

*edited the unclear/confused line
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Seetharaman Venkatasamy wrote:
saloni jhanwar wrote:what do you think ?

well. polymorphism- apply same method on different objects and behavior changes depends on objects(overriding).
in case of over loading, we are not applying same methods on different objects. example void method(int) is not method(String)...

I know, still some programmer say overloading as compile time polymorphism. I dont agree with them


Yes, and i am one of them. In overloading concept why we are using same name? if you think every method is different then why don't we use unique name for each method ? You don't think it is enough to have polymorphic behavior(same name), One thing many form and i believe it is here.thanks
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I second Saloni's thought that there is a compile time polymorphism where method calling is rather determined at compile time than runtime and that can be determined using overloaded methods.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Consider this example:


I think I have bit messed in my previous reply. sorry.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

saloni jhanwar wrote:
Seetharaman Venkatasamy wrote:
In overloading concept why we are using same name?

technically method name is its signature. signature = method name and the parameter list (ordered)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Seetharaman Venkatasamy wrote:

I think I have bit messed in my previous reply. sorry.


I'm not asking example, i know overloading.You should reply of previous post.You're just thinking only in one way as we usually work with polymorphism but polymorphism definition doesn't bound with only it that we will always call only this type of scenario polymorphism not anything else.thanks



Overloading is compile-time polymorphism because it is during compile time that the JVM figures out what method to call by checking what was passed in the arguments to the overloaded method.
source

source
Guilherme Bazilio
Greenhorn

Joined: Mar 02, 2009
Posts: 23
Seetharaman Venkatasamy wrote:
saloni jhanwar wrote:
Seetharaman Venkatasamy wrote:
In overloading concept why we are using same name?

technically method name is its signature. signature = method name and the parameter list (ordered)


Reading Sierra's book, in chapter 2 is said: "Polymorphism applies to overriding, not to overloading."

And, as Seetharaman wrote, method is not identified only by it's name, but name + argument list. And invocation of these methods is based on the type of the actual instance on the heap, which is only possible to happen at runtime!
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Guilherme BazĂ­lio wrote:
...


I am not denying run time polymorphism but you are also thinking in only one way, read theory about polymorphism and then ask yourself why it shouldn't be with compile time ? because book said it is not good reason.polymorphism can be achieve with not only one way, read mentioned sources.

I would like to repeat here.

You're just thinking only in one way as we usually work with polymorphism but polymorphism definition doesn't bound with only it that we will always call only this type of scenario polymorphism not anything else.
Guilherme Bazilio
Greenhorn

Joined: Mar 02, 2009
Posts: 23
Because book said isn't a good reason, for sure, but the book itself presented and proved really good reasons for that.

What sources did you read or what makes you think that there is a "compile time polymorphim"? For now, I just can't think of polymorphim being at compile time!

Let me "open my mind"! Hehe

Thanks!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

There's no definitive answer to this, and plenty of disagreement - you can probably find lots of previous threads about it. I personally don't consider overloading to constitute polymorphism. But plenty of people do. It comes down to exactly how you define "polymorphism".
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Guilherme Bazilio wrote:Because book said isn't a good reason, for sure, but the book itself presented and proved really good reasons for that.

What sources did you read or what makes you think that there is a "compile time polymorphim"? For now, I just can't think of polymorphim being at compile time!

Let me "open my mind"! Hehe

Thanks!


You need study about polymorphism theoretically then only your mind can be opened ,and this is not only matter of sources or book just read polymorphism definition and think, is there any boundation about particular type scenario ??. Come on you can think about it or just google you will get many theory related polymorphism, this it is not so tough.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

saloni jhanwar wrote:
Guilherme Bazilio wrote:Because book said isn't a good reason, for sure, but the book itself presented and proved really good reasons for that.

What sources did you read or what makes you think that there is a "compile time polymorphim"? For now, I just can't think of polymorphim being at compile time!

Let me "open my mind"! Hehe

Thanks!


You need study about polymorphism theoretically then only your mind can be opened ,and this is not only matter of sources or book just read polymorphism definition and think, is there any boundation about particular type scenario ??. Come on you can think about it or just google you will get many theory related polymorphism, this it is not so tough.



First, let's keep this conversation civil shall we?

Anyway, technically, neither method overloading or method overriding is polymorphism -- although it can be argued that the method overriding feature is more important for polymorphism (which arguable is dependent on which definition of polymorphism we are referring to)..... Having said that, does it really matter that there is a distinction between compile time and run time?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Henry Wong wrote:
saloni jhanwar wrote:
Guilherme Bazilio wrote:Because book said isn't a good reason, for sure, but the book itself presented and proved really good reasons for that.

What sources did you read or what makes you think that there is a "compile time polymorphim"? For now, I just can't think of polymorphim being at compile time!

Let me "open my mind"! Hehe

Thanks!


You need study about polymorphism theoretically then only your mind can be opened ,and this is not only matter of sources or book just read polymorphism definition and think, is there any boundation about particular type scenario ??. Come on you can think about it or just google you will get many theory related polymorphism, this it is not so tough.



First, let's keep this conversation civil shall we?

Anyway, technically, neither method overloading or method overriding is polymorphism

Henry


Yes,if polymorphism theory is not applied here then we can call it only a technique to achieve something but polymorphic behavior is found so it can be applied.thanks
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

i remember falling into trap of this question on some other forum and there was lots of people arguing and lots of exchange of words. eventually i was not satisfied with a single answer as to is there any compile time polymorphism in java or not. worst i even forgot what is polymorphism. there is also a previous thread going on here at http://www.coderanch.com/t/565139/java/java/compile-time-polymorphism. i think the things in this post are going in the same direction as the link mentioned above in that HOW WILL YOU DEFINE POLYMORPHISM ? so i guess unless that thing is clear we cannot give answer to original question.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

gurpeet singh wrote:i remember falling into trap of this question on some other forum and there was lots of people arguing and lots of exchange of words. eventually i was not satisfied with a single answer as to is there any compile time polymorphism in java or not. worst i even forgot what is polymorphism. there is also a previous thread going on here at http://www.coderanch.com/t/565139/java/java/compile-time-polymorphism. i think the things in this post are going in the same direction as the link mentioned above in that HOW WILL YOU DEFINE POLYMORPHISM ? so i guess unless that thing is clear we cannot give answer to original question.


Hmm, it is not trap it is always good to discuss something,and it can't be ignored that there is no polymorphic behavior is found in overloading.It is not bad to allow possibility which is really here, instead of to be negative and ignorance of possibility.and it doesn't matter how we accept it as Henry said,run time polymorphism is more important so may they didn't find it as important to define overloading as polymorphism.that's what i wanted to say.
Guilherme Bazilio
Greenhorn

Joined: Mar 02, 2009
Posts: 23
This topic discuss about static and dynamic polymorphism. It's really interesting and "opened my mind".

Seems like it's only a matter of concepts (with different names).

Compile Time Polymorphism seems to be the same as Static Polymorphism which seems to be the same as Early Binding.

Runtime Polymorphism seems to be the same as Dynamic Polymorphism which seems the same as Late Binding.

But, as far as I've understood, polymorphism is: something that assumes many forms.

And..as it seems, polymorphism in java is: an object from the heap that can be referenced by different variables types (once it extends or implements that class).



What do you guys think?
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Guilherme Bazilio wrote:This topic discuss about static and dynamic polymorphism. It's really interesting and "opened my mind".

Seems like it's only a matter of concepts (with different names).

Compile Time Polymorphism seems to be the same as Static Polymorphism which seems to be the same as Early Binding.

Runtime Polymorphism seems to be the same as Dynamic Polymorphism which seems the same as Late Binding.

But, as far as I've understood, polymorphism is: something that assumes many forms.

And..as it seems, polymorphism in java is: an object from the heap that can be referenced by different variables types (once it extends or implements that class).



What do you guys think?


Yes, this will be always a good debate topic, which will ultimately lead as null
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: run-time and compile-time