• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Overriding in runtime???

 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one please explain me details why overriding happends only in runtime? Why not in compile time? Compilar knows all the details which object will create... So why can't a compilar do it?


Please Explain... Waiting egarly for your answers...

Thank You so much...

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Damodar Mukhopadhyay:
...Compilar knows all the details which object will create...

Does it?
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please discuss how the JVM manages the overriding.
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compile time can also do some thing like this.




The above code is not complited. It is just demo.

In the above code how the compiler will decide... ( Suppose that type of situation...)


Help Please...
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above code how the compiler will decide...

Will decide what? There's nothing in that example that relates to overriding (nor is there, actually, in Marc example, which demonstrates polymorphism).
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yah thats true theres nothing overriding!!!

I want to say that if this example is decided by the compiler then why
runtime decide overriding...!!!

Got my point?
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Damodar Mukhopadhyay:
Yah thats true theres nothing overriding!!!

I want to say that if this example is decided by the compiler then why
runtime decide overriding...!!!

Got my point?


I think you are missing the point. You can't show one example, that in theory, the compiler can figure out the exact method to do the virtual call to, and assume that the compiler can do it for all method calls.

Polymorphism is done during runtime, because in most cases, it can't be done during compile time.

Henry
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yah thanks. that I know. But problem is that in some interview I was not able to impress the interviewer.
Can any one please tell me the appopriate answer.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps we're not understanding what you are asking. I THINK you are asking "Why does overriding happen at runtime?"

The answer given is "Because sometimes the compiler CAN'T KNOW which method to call, so it HAS to be done at runtime."

You don't seem satisfied with this answer. So, either we are not undertanding what you are really asking, or there is some other mis-communication going on.
 
Louis Moloney
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
over riding happens at runtime because the compiler does not know what object a ref refers to on the heap:

eg in a method you have

void myMethod(List list){
System.out.println(list.size());
}

is it an arraylist, is linkedlist ?

i know its not overriding here but calling an implemented method.
but the concept is exactly the same.

Compiler does not know what type of object we calling size() on.
This is done at runtime.
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need nome low level information. Can any one please discuss it?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are requesting is beyond the scope of SCJP. Moving to Java In General...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what kind of low level information do you want? are you asking HOW the runtime environment does it?

"Why" is not a low level question...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic