Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

run-time and compile-time

 
John Paterson
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is known as compile time and run time polymorphism.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this example:


I think I have bit messed in my previous reply. sorry.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic