File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic binding

 
Rajiv Rai
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Vishal Shaw
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • 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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic