• 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

Method overriding

 
Greenhorn
Posts: 23
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why Method overriding is not decided at compile time?
What is the necessary to decide at run time only?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hs Raveendran wrote:Why Method overriding is not decided at compile time?
What is the necessary to decide at run time only?


hello dude.....
The compiler only analyses your code for syntax errors,semantic errors or lexical errors and convert it to machine code.
It is only at run time,your code executes.Main class is invoked,methods are called etc.Method overidding comes into scene,if you had made a subclass.Java runtime system calls the overridden methods.Method overriding is just a concept.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because often it is not known before runtime which method needs to be called.

Suppose Jack wrote the following code:

Jack compiles this and now has a *.class file. Note that at the moment of compiling the above class, the compiler cannot know what class will implement Runnable which will be passed to the runSomething() method.

Chris might write some code tomorrow that uses Jack's class:

This class didn't even exist when Jack compiled his code yesterday - Jack's compiler could not look into the future and see that Chris would write this code today, Jack's compiler couldn't know that the Program.run() method in Chris's code would exist. So it's impossible that Jack's compiler could have decided which run() method exactly to call.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent explanation Jesper !
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A very simple example that uses reflection to create the actual List type, a technique not unusual with plugins. Now, which implementation of add should the compiler use? That of LinkedList? ArrayList? Some other custom List?

JDBC has it even worse, where you only mostly interfaces for reference types.


And of course Jesper gives a better example before mine
 
reply
    Bookmark Topic Watch Topic
  • New Topic