• 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

What is the advantage of Dynamic-Binding/Runtime-Polymorphism in Java?

 
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

or


What advantage you get if you use parent's reference variable to create child's object then call child's method?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java uses static binding to determine what overloaded version of a method needs to be called, and it uses dynamic binding to determine what overridden version of a method needs to be called.

The static binding that the compiler does is the same for your two examples, because the argument list of the call to show() hasn't changed. The dynamic binding that the virtual machine does is also the same for your two examples, because the runtime type of the object you're calling show() on hasn't changed.

In short, your examples don't demonstrate any kind of polymorphism, except that you can assign a reference of a subtype to a variable of a supertype.

The advantage of using variables of the least specific type that you need is that you can easily change the runtime type of the object it references, without having to change the type of the variable itself.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic