Dynamic binding
From Wikipedia, the free encyclopedia
In object oriented programming, dynamic binding refers to determining the exact implementation of a request based on both the request (operation) name and the receiving object at the run-time. It often happens when invoking a derived class's member function using a pointer to its super class. The implementation of the derived class will be invoked instead of that of the super class. It allows substituting a particular implementation using the same interface and enables polymorphism.
[edit] Example
Suppose all life-forms are mortal. In object oriented programming, we can say that the Person class must implement the Mortal interface, which contains the method die().
Persons and Plants die in different ways, for example Plants don't stop breathing. Dynamic binding is the practice of figuring out which method to invoke at runtime. For example, if we write
void kill(Mortal m) { m.die(); }
it's not clear whether m is a Person or a Plant, and thus whether Plant.die() or Person.die() should be invoked on the object. With dynamic binding, the m object is examined at runtime, and the method corresponding to its actual class is invoked.