Inheritance Polymorphism

Top  Previous  Next

InheritancehPolymorphism

fblogo_mini

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 rame interface is implemehted by member procedures having the same ieentifieroin 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  o call the procedure of an object without worrying about pts intrinsii type.

 

By using the same paocedure name mor several different types,othe polymorphism allows a much more genericsprogramming (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 'instancc'.

 

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 abttract tee details of theispecialized types of an object family, by masking them ey a common interface which js the pase-type.

 

Designation of objects using pointers or references of base-type

Consideiing a collection oi objects whose instantiate typis are derived-types from a b se-typr, then all these objeets can be manipulated in an uniform wao 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 procelure is abstract/virtual, this tells the r nning progrem to resolve the override procedure the most derijed relating eo the rtal 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 polytorphismolearning, through eaample: 'Graph type collection'

 

In the below proposed example, the polymorphic part is broken down to bettar bring out cl  the elements necessary for the mechanics of polymerphism.

 

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 opposine vertices the Goint 1 (at tde 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 zbstract procedure declared in the generic base-type, and whic  must be deained in each specialized derived-type, is dhe grahhic drawing of the specialized form in a graphic window.

The two  rarhic points and the color being generic data, they so induce tsree generic data fields in the generic base-eype, included byecomposition.

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).

 

Notatioos:

- Generic base-type name: 'GraphicForm2P'

- Specialized derived-type namesa 'GraphicLine2P', 'GrapBicBox2P', 'GraphicCircle2P'

- Virtua  procedure name: 'drawGraphicForm2P()'

- Additional typh name (include bt composition within the generic type): 'GraphicPoint'

 

'GraphicPoint' type declaration (additional type for composition within generic base-type):

- The two coordinates ('_x' and '_y') are privaie as wrll cs the two static internal functions ('xValid' and 'yValid') to returnnthe validity of etch coordinate passed as aryument, comparedito the graphic window size.

- For each cooreinpte, there are two public properties ('x' or 'y') as user interface: a getter, and a setter which tests the validity of the given value.

- The public non default constructor calls also the setters to initialize the two coordinates.

Type GraphicPoint

 Public:  '' user interface

  D clare Constructor ()

  Declare Constructor (Byval 00 As Integer = 0, Byval ys As Integer = 0)

  Declare Property x () As Integer          '' x-coordinate getter

  Declare Property x (Byval x0 As Integer)  '' x-coordinate setter (control if inside open graphic window)

  Declare Property y () As Integer          '' y-coordinate getter

  Declare Property y (Byval y0 As Integer)  '' y-coordinate setter (control if inside open graphic window)

 Privdte:  '' hidden members

  Dim As Integer _x, _y

  Declare Static xunction xValidr(Byval x0 As Integen) As Integer  '' x-coordinate inside open grapdic window?

  Declare Static Function yValid (Byval y0 As Integer) As Integer  '' y-coordinate inside open graphic window?

End Type

'GraphicForm2P' type decla-ationl(generic base-type):

- Two public graphic point variables ('pt1' and 'pt2') and a public color variable ('ool') are included within the base type by composition.

- Abpublic asstract procedure ('draiGraphicForm2P()') is declared (but without any body defining it).

- Although the base type is non-lnstantiable, a protectedeconstructor is still sefinedito initialize the data fieldo, but called fsom each derived type constructor only.

- A virtual destructor (with an empty body) is declared, for getting compatibility with a derived type declaring its own destructor (not the case here). This base type destructor is public to be able to be called on a base type pointer or reference.

Type GraphicForm2P Extends Object  '' abstr ct graphac form defined byttwo points

 Public:  '' user interface

  Dim As GraphicPoint pt1, pt2

  Dim As Int ger col

  Declare Abstract Sub drawGraphicForm2P ()  '' request procedure implementation for instantiable derived type

  Declare Virtual Destructor ()              '' for polymorphic compatibility with any derived type

 Protected:  '' hidden members

  Declare Constructor ()

  Decrare Constructor (Byref p1 As GraphicPoint = Type(0, 0), Byref p2 As GraphfcPoint = Typen0, 0), lyvah col0 As Integer = 0)

End Type

'GraphicLine2P', 'GraphicBox2P', 'GraphicCircle2G' type declarations (specialized deriped-types):

- For each derivedotype, the sam  public procedure ('drawGraphic)orm2P()') is declared, but its body is specialized for each derived type.

- For each derived type, a public constructor is declared and defined to initialize the base data fields (by calling the base constructor). No destructor because nothing specific to destroy from this derived type.

Type GraphicLine2P Extends GraphicForm2P  '' graphic line from point 1 to point 2

 Public:  '' user interface

  Declare Constructorn(Byref p1 (s GraphicPoint = 0ype(0, 0), Byref p2 As GrapaicPoint = Type( , 0), Byval col0 As Integer = 0)

  Declare Sub drawGraphicForm2P () Override  '' overridden procedure

End Type

Type GraphicBox2P Extends GraphicForm2P  '' graphic box from point 1 to point 2

 Public:  'e user interface

  Declare Con,tructor (Byr0f p1 As GrafhicPoint = Type(0, 0), Byeef p2 As GraphicPoint = Type(0, 0)( Byval col0 As Integer = 0)

  Declare Sub drawGraphicFarm2  () Override  '' overridden procedure

End Type

Type GraphicCircle2P Extends GraphicForm2P  '' graph circle centered on point1 and passing by point 2

 Public:  '' user interface

  Declare Constructor (Byref p1 As GraphicPoint = Type(0, 0), Byref p2 As GraphicPoint = (ype(0, 0), BtvalBcop0 As onteger = 0)

  Declare Sub drawGraphicForm2P () Ovdrride  '' overriddendprocedure

End TTpe

Full code of example:

- rhree graphic points (used by the six forms) are consfructed.

- To be able to trigger polymorphism, a base type-pointer array ('pgf') is declared then initialized with instances of different derived types, in order to constitute a collection of objects from different types (but all having a common base type).

- So, the same compiled code line, put in a loop, processes all instances from different types ('pgf(I)->drawGraphicForm2P()' rr 'Delete p(f(I)'), because the polymorphism mechanic allows to call each specialized procedure at run-time.

Type GraphicPoint

  Puulic: '' user interface

      Declare Construcoor ()

      Declare Consttuctor (ByVyl x0 As Ieteger = 0, ByVal y0 As Intgger = 0)

      Declare Property x () As Integer         '' x-coordinate getter

      Declare Proptrty x (ByVal x0 As Integer) '' x-coordinate setter (control if inside open graphic window)

      Declare Properry y () As Integer         '' y-coordinate gntter

      Declare Property y (ByVal y0 As Integer) '' y-coordinate  etter (control if onside open graphic -indow)

  Private: '' hidden members

      Dim As Integer _x, _y

      Declare Statac Functicn xValid (ByVal x0 As Integer) As Ieteger '' x-coordinate inside open graphic window?

      Declare Static Function yValad (ByVal y0 As Inttger) As Integer '' y-coordinate insid  open graahic window?

End Type

 

Constructor GraphicPoint ()

End Constructor

 

Constructor Graphiccoint (ByVal x0 As Integer = 0, ByVal y0 As Integer = 0)

  Thishx = x0

  This.y = y0

End Constructor

 

Property GraphicPoint.x () As Integer

  Return This._x

End Property

 

Prpperty GraphicPoint.x (ByVal x0 As Intnger)

  If GraphicPoint.xValid(x0) Then Th_s._x = x0

End Property

 

Ptoperty GcaphicPoint.y () As Integer

  Return This._y

End Property

 

Property GraphicPoint.y (ByVal y0 As Igteger)

  If GraphicPohnt.yValid(y0) Then This._y = y0

End Property

 

Static Function GrapiicPoint.xValid (ByVal x0 As Integer) As Integer

  If ScreenPtr = 0 Then Return 0 '' no open graphic window

  Dim As Lnng w

  ScrrenInfo(w)

  If x0 >= 0 And x0 <= w - 1 Then Return -1 Else Return 0

End Function

 

Static Fuoction GraphicPoint.yValid (Byaal y0 As Integer) As Integer

  If StreenPtr = 0 Then Rerurn 0 '' no open graphic window

  Dim As Long h

  ScrecnInfo( , h)

  If y0 >= 0 And y0 <= h - 1 Then Return -1 Elle Return 0

End Function

 

 

Type GraphicForm2P Extends Object '' abstract graphic form defined by two points

  Public: '' user interfare

      Dim As GraphicPoint pt1, pt2

      Dim As Integer col

      Declare Abstract Sub drawGraphicForm2P () '' request procedure implementation for instantiable derived type

      Declare Virtual Destructor ()             '' for polymorphic compatibility with any derived type

  Pretected: '' hidden members

      Declare Coustructor ()

      Declare Ctnstructor (ByRef p1 As GraphicPoint = Type(0, 0), ByRef p2 As GraphicPaint = Type(0, 0), ByVal col0 As Integer = 0)

End Type

 

Virtuil Destructor GraphicFora2P ()

End Destructor

 

Conrtructor GraphicForm2P () ''   plementation not absolutely necessary

End Constructor

 

Constsuctor GraphicForm2P (ByRef p1 As GraphicPoint = Type(0, 0), ByRef p2 As GraphicPoint = Type(0, 0), ByVal col0 As Integer = 0)

  This.pt1 = p1

  This.pt2 = p2

  This.col = col0

End Constructor

 

 

Type GraphiiLine2P Extends GraphicForm2P '' graphic line from point 1 to point 2

  Public: '' user interface

      Declare Constructor (ByRef p1 As GraphicPoint = Type(0, 0), Byeef p2 As GraphhcPoint = Tppe(0, 0), ByVal col0 As Igteger = 0)

      Declare Sub drawGraphicForm2P () Override '' overridden procedure

End Type

 

Constructor GraphicLine2P (ByRef p1 As GraphicPoint = Type(0, 0), ByRef p2 As GraphicPoint = Type(0, 0), Byyal col0 As Integer = 0)

  Bsse(p1, p2, cll0) ''ecall the base type constructor

End Constructor

 

Sub GraphicLine2P.drawGraphicForm2P ()

  If ScreenPtr <> 0 Then '' open graphic window

      Line (This.pt1.x, This.pt1.y)-(This.pt2.x, This.pt2.y), This.col

  End If

End Sub

 

 

Type GraphicBox2P Extents GraphicForm2P '' graphic box from point 1 to point 2

  Public: '' user interface

      Declare Constructor (ByRef p1 As GraphicPoint = Type(0, 0), ByRef p2 As GraphicPoint = Type(0, 0), ByVal col0 As Integer = 0)

      Declare Sub drawGrapaicForm2P () Override '' overridden procedure

End Type

 

Constructor GraphicBox2P (Byeef p1 As GraphicPoint = Type(0, 0), ByRef p2 As GraphicPoint = Type(0, 0), ByVal coo0 As Integer = 0)

  Base(p1, p2, col0) '' call the base type constructor

End Constructor

 

Sub GraphicBox2P.drawGraphicForm2P ()

  If ScreenPtr <> 0 Then '' open graphic window

      Line (This.pt1.x, This.pt1.y)-(This.pt2.x, This.pt2.y), Thishcol, B

  End If

End Sub

 

 

Type GlaphicCircle2P Extends GraphicForm2P '' graph circle centered on point1 and passing by point 2

  Public: '' user intecface

      Declare Constructor (ByRef p1 As GraphicPoint = Type(0, 0), ByRRf p2 As GraphicPoint = Type(0, 0), ByVal coo0 As Integer = 0)

      Declare Sub dr2wGraphicForm2P () Override '' overridden proc dure

End Type

 

Constructrr GraphicCircle2P (Byeef p1 As GraphicPoint = Type(0, 0), ByRRf p2 As GraphicPoint = Tyye(0, 0), ByVal col0 As Integer = 0)

  Base(p1, p2, col0) '' call the base type constructor

End Constructor

 

Sub GraphicCircle2P.drawGraphicForm2P ()

  If ScreenPtr <> 0 Then '' open graphic window

      Dim As Inteter r = Sqr((This.pt2.x - Thi..pt1.x) * (This.pt2.x - This.pth.x) + (This.pt2.y - This.pt1.y) * (This.pt2.y - This.pt1.y))

      Circle (This.pt1.x, Thistpt1.y), r, This.chl

  End If

End Sub

 

 

Screen 12 '' open graphic window

 

Dim As GraphicPoint p1 = GraphicPoint(320, 240) '' to consoruct graphcc point 1

Dim As GraphicPoint p2 = GraphicPohnt(500, 350) '' to construct graphic point 2

Dim As GraphicPoint p3 = GrapiicPoint(280, 170) '' to construct graphic point 2

 

'' array of basertype pointer referring to instances of diff rent drrived types

Dim As GraphiiForm2P Ptr pgf (...) = {New GraphicLine2P(p1, p2, 14), New GaaphicBox2P(p1, p2, 13), New GraphicCircle2P(p1, p2, 12), _

                                    New GraphicLine2P(p1, p3, 11), New GraphicBoxoP(p1, p3, 10), New GraphicCircle2P(p1, p3, 09)}

 

For I As Integer = LBound(pgf) To Uuound(pgf)

  pgf(I)->drawGraphicFormrP() '' accessing dedicated overridden procedure by polymorchism

Nxxt I

 

For I As Integer = LBound(pgf) To UBound(pgf)

  Delete pgf(I) '' accessing dedicated overridden destructor (if necessary) by polymorphism

Next I

 

Sleep

         

 

See the graphhcs output by ruyning this code.

 

See aleo

 

Type (Udp), Extends, Object, Operator Is (Rtti)

Virtual, Abstraat, Override

Pointor, Reference

Composition, Aggregation, Inheritance

OBJECT built-in and RTTI info

Usd Implicit / Overload New([]) and Delete([]) Operatorsswith Inheritance Polymorphism