Static vs dynamic binding - virtuals

When extending a class definition, new features are added to those in the parent class. This is sometimes called concatenation of the two sets of features. As long as the names used are all distinct, no problems occur. If the sub-class uses names which are already defined in the parent, the effect depends on the way the language used resolves the conflict. There are basically two different views that are taken. Some languages allow both, others allow only one.

Statically bound definitions are hidden in the sub-class by a new definition with the same name. This is the familiar scope rule used in Pascal when nested procedures are declared. Any use of the name in the parent class is still bound to the definition within that class, which means that its use must be as was intended by the writer of that class. Uses within a sub-class where it is redefined and within any classes which are sub-classes of that are bound to the definition in the sub-class.

Dynamically bound definitions (virtual definitions) are also hidden in the sub-class by a redeclaration at that level. However, they are replaced in the parent class as well. This is very useful where, for instance, a family of object types all have the ability to be displayed but the effect of this differs for each of them. By noting in the parent class that such an operation is mandatory and that it has a certain form, we are free to use it in the parent class and assume each specialisation will provide an over-riding definition which replaces it when used. In some languages the parent class need not provide a default implementation, in others it must always do so.

Most languages in common use have adopted a variation of SIMULA's combination of default static binding with an explicit option to make certain operations dynamically bound. C++ is one of these and uses the virtual specifier to indicate dynamic binding.


Next note in series

Back to index