File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why Overriding in runtime??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why Overriding in runtime???" Watch "Why Overriding in runtime???" New topic
Author

Why Overriding in runtime???

Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
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...



SCJP 1.5, SCWCD 1.4, SCBCD 5.0, next SCDJWS...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Damodar Mukhopadhyay:
...Compilar knows all the details which object will create...

Does it?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
Can you please discuss how the JVM manages the overriding.
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
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
Marshal

Joined: Mar 22, 2005
Posts: 41180
    
  45
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).


Ping & DNS - my free Android networking tools app
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
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
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

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


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

Joined: Jan 15, 2007
Posts: 98
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

Joined: Oct 02, 2003
Posts: 11172
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
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

Joined: Jan 15, 2007
Posts: 98
I need nome low level information. Can any one please discuss it?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What you are requesting is beyond the scope of SCJP. Moving to Java In General...


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

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...
 
jQuery in Action, 2nd edition
 
subject: Why Overriding in runtime???
 
Similar Threads
Connecting to a Database
Covariant problem
Overriding doubt in K&B
output of program?
how to retrieve the table list in Access database through JDBC?