• 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

Static binding Vs Dynamic Binding

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a code snippet


And output is

Shouldnt both method calls give Child Parent Version using Dynamic binding
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think in this particular ? there is no need of binding coz the method is invoked using the same reference.
You are passing reference var to the methods and it chosses the method that is most specific to the argument type passed.
So that explains the nature of o/p.
Am i right??
 
seshu kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya,
It looks like a dynamic binding but isnt.
It is a case of pure method invocation and i think it is resolved at compile time itself.
Similar example

Whats will the result be?
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String version will be printed as that is more specific down the inheritence.
But if u replaced the Object bit by an object of StringBuffer it will give error. Coz though even StringBuffer can have null value but it does not belong to the hierarchy so becomes ambigious.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
seshu,
in your first code snippet, I don't see the definition of method in neither the Parent class nor the Child class. therefore, the method from the Test class is invoked depending on the argument type.
The general rule for dynamic method lookup is that the method invoked, depends on the class of the reference, and not its compile-time type. Had you had a definition of method in both your Parent and Child classes, then dynamic binding will go to work and will call the methods from appropriate classes.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the JLS:
When a method is invoked (�15.12), the number of actual arguments and the compile-time types of the arguments are used, at compile time, to determine the signature of the method that will be invoked (�15.12.2).
So for overloaded methods, you use the object reference type, not the type of the object at runtime, in this particular case.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic