aspose file tools*
The moose likes Beginning Java and the fly likes Dynamic binding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dynamic binding" Watch "Dynamic binding" New topic
Author

Dynamic binding

Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 09, 2012
Posts: 179
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!!!

Programming is about thinking, NOT coding
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic binding