• 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

Why Overriding in runtime???

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need nome low level information. Can any one please discuss it?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are requesting is beyond the scope of SCJP. Moving to Java In General...
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic