• 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

Dynamic binding

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

I have been reading Headfirst Java book.

In the first chapter , there is a section where there is
a fireside chat between JVM and compiler as to which is
more important.

In the chat , it is written that the compiler has to allow some data type exceptions
to support data type binding. At runtime, a java program can include new objects
that were not even known to the original programmer.

Can somebody please tell the example of such objects which are included at runtime
and how is it related to dynamic binding

Thanks
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One classic example is JDBC, with the interfaces and classes in the java.sql package.



Connection, PreparedStatement, and ResultSet are all interfaces. When I'm writing that code, I don't know and I don't care what the concrete classes are that will actually be implementing them at runtime. All I care about is, for example, that a Connection allows to me to call perpareStatement() to produce a PreparedStatement, that a PreparedStatement allows me to call executeQuery(), and so on.

Dynamic binding is when, at runtime, the JVM binds those method calls to the concrete objects that I didn't know about and whose classes may not even have been present at compile time.
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajiv,

Continue reading and you will get your answer in the book itself. Right now I can give you a small overview on it, assuming you know inheritence. You get dynamic binding , when the compiler has no idea about the type of object being assigned. The compiler assumes that the assignment is correct based on the hierarchy presented to it at compile time . Then at runtime , the VM checks the binding and takes necessary actions. For example, I can cast an Object to any class, like

When you compile this , the compiler assumes that since Object is the super class of all classes, so the casting is valid. But at runtime , the VM checks and reports an Exceprion as obj cannot be casted to Integer.

Hope this helps.

Cheers!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic