Inheritance Polymorphism


The Inheritance Polymorphism is the ability of calling from the base-type the member procedures of derived-types without worrying about the real type of the processed objects.

Preamble:
Inheritance polymorphism (sub-type polymorphism) is the concept of providing a single interface to entities that can have different types.
More precisely, a same interface is implemented by member procedures having the same identifier in each type belonging to the same inheritance hierarchy.

Thanks to the 'abstract'/'virtual' procedures, one can write a code using only the base-type that will automatically call the derived-type procedures.
It is then possible to call the procedure of an object without worrying about its intrinsic type.

By using the same procedure name for several different types, the polymorphism allows a much more generic programming (abstraction).
The coder does not have to know, when calling a base procedure, the precise type of object on which the procedure will apply. He just needs to know that this type will implement the procedure.

For example a procedure 'moving()' will perform the appropriate movement according to the real derived-type of the instance referenced at the time of the call. This will allow the program to say 'instance.moving()' without having to worry about the real derived-type of 'instance'.

Inheritance polymorphism operating
The ability to redefine a procedure in a derived-type inheriting from a base-type is called specialization.
It is then possible to call the procedure of an object without worrying about its intrinsic type: it is the inheritance polymorphism.

This makes it possible to abstract the details of the specialized types of an object family, by masking them by a common interface which is the base-type.

Designation of objects using pointers or references of base-type
Considering a collection of objects whose instantiate types are derived-types from a base-type, then all these objects can be manipulated in an uniform way by considering them as objects of the base-type.
Better, certain behaviors can be specialized according to the instantiate type of each object. In other words, the use of distinct objects of the same inheritance hierarchy is homogeneous even if the behavior of these objects remains specific.

Thus, a base-type pointer or reference, pointing to an instance of a derived-type, can be used to manipulate such an object.

Overriding the abstract/virtual procedures in the base-type by specialized procedures in derived-types
To can declare abstract/virtual procedures in a type, this type must 'Extends' (directly or indirectly) the built-in 'Object' type.

A derived-type can override an abstract/virtual procedure declared in its base-type, by declaring a procedure with the same identifier and signature, meaning same number and type of parameters, same calling convention, and if any, same return type (or a return of a derived-type for return by reference or by pointer):
- Normally a base-type reference/pointer can access only a procedure in the same type or in a type upper in hierarchy (static binding at compile-time), even if this reference/pointer refers to an object of instantiate type derived from the base-type.
- But when the base-type procedure is abstract/virtual, this tells the running program to resolve the override procedure the most derived relating to the real object type (dynamic binding at run-time).

Restriction:
Polymorphism is not directly compatible with:
- any operators 'New[]' or 'Delete[]' (the array versions for statement/expression/overload operators) because the use of a sub-type pointer (instead of the real type) fails for accessing the other elements (except the first),
- even the overload operator 'Delete' is not directly compatible because it can not be declared virtual (being static).

Instead of having to call such an operator 'Delete([])' statement on derived-type pointer, the safest way is to simply call (on base-type pointer) an overridden user virtual member procedure that will automatically launch the operator 'Delete([])' statement at derived-type level.

Inheritance polymorphism learning, through example: 'Graph type collection'
In the below proposed example, the polymorphic part is broken down to better bring out all the elements necessary for the mechanics of polymorphism.

The generic base-type chosen is any 'Graphic Form' defined by two graphic points and a color (abstraction).
The specialized derived-types are a 'Graphic Line', a 'Graphic Box', and a 'Graphic Circle' (all defined by two graphic points and a color):
- The 'Graphic Line' connects the point 1 and the point 2.
- The 'Graphic Box' has as opposite vertices the point 1 (at the top and on the left) and the point 2 (in bottom and on the right).
- The 'Graphic Circle' has as center the point 1 and go through point 2.

The abstract procedure declared in the generic base-type, and which must be defined in each specialized derived-type, is the graphic drawing of the specialized form in a graphic window.
The two graphic points and the color being generic data, they so induce three generic data fields in the generic base-type, included by composition.
A 'graphic point' type is also defined with encapsulation of the x/y coordinate values (declared private), in order to control their validity (depending on the defined graphic screen size) by means of properties (but public these ones).

Notations:
- Generic base-type name: 'GraphicForm2P'
- Specialized derived-type names: 'GraphicLine2P', 'GraphicBox2P', 'GraphicCircle2P'
- Virtual procedure name: 'drawGraphicForm2P()'
- Additional type name (include by composition within the generic type): 'GraphicPoint'


See also
Back to Programmer's Guide
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode