In the first chapter , there is a section where there is
a fireside chat between JVM and compiler as to which is
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
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.
Joined: Aug 09, 2012
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.