• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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.

 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic