• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

method overloading and class overriding

 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a basic doubt...

Why does the runtime behaviour differ in the above 2 cases?
Overriding a method thro' inheritance seems pretty natural to me.
But the behaviour in the other case (method overloading) puzzles me.
Why runtime type does'nt matter there?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short answer: Because the language designer decided to resolve parameter types at compile time.
Longer answer: It would have been possible to resolve parameter types at runtime, but it's a little bit tricky. For example, you can easily get ambigious situations:
having methods with signatures
foo(Object, String)
foo(String, Object)
which one should be called by
foo("string", "string"); ?
The language designer probably thought that it would be better to not let such problems happen at runtime.
BTW, if you need this form of runtime polymorphism, you should take a look at the Visitor Design Pattern aka Double Dispatch.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, i found an article on multiple dispatch on the web.
BTW, i was wondering if there are languages that support this kind of runtime behaviour?
thanks.

Originally posted by Ilja Preuss:
BTW, if you need this form of runtime polymorphism, you should take a look at the Visitor Design Pattern aka Double Dispatch.

 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic