View RSS Feed

Petr Schreiber

UDT inheritance: Accessing base type functions

Rate this Entry
ThinBASIC user defined types, also known as UDTs, offer convenient way to inherit properties and functions - via the Extends keyword.
This approach allows inheriting properties and functions from the base UDT. This is very useful, but there is one catch one needs to be aware!

If you specify function in the new UDT which has the same name as function in the base UDT, override is performed and the whenever you use the function, the one from the new type is used.
This is expected behavior but there are situations when you still need to access the original base function. Let me illustrate this on the following example:
Type Point2D
  x As Single
  y As Single

  Function Initialize(x As Single, y As Single) 
    Me.x = x
    Me.y = y
   End Function

  Function ToString() As String
    Return StrFormat$("{1},{2}", Me.x, Me.y)
   End Function
End Type
Type Point3D Extends Point2D
  z As Single

  Function Initialize(x As Single, y As Single, z As Single) 
    Me.x = x
    Me.y = y
    Me.z = z
   End Function

  Function ToString() As String
    Return StrFormat$("{1},{2},{3}", Me.x, Me.y, Me.z)
  End Function
End Type
You can see the Point2D and Point3D differ basically just by added z property in the 3D version. What if you wanted to recycle the Point2D Initialize and ToString functionsto simply build upon them in new UDT?
If you try to call any of these, the variant for Point3D will be used, thanks to override mechanism described earlier.

So how do I access the base methods? Can it be done at all?
What helps to resolve this situation is to understand that Point3D is basically Point2D + something new. Even at binary level. If you are thinking about AT overlay now... yes, that is the answer
Using DIM .. AT with base type allows us to use the functions, even if they collide at name level with the new type names. And without any overhead, because overlay variables do not allocate memory.

Here comes the modified example:
Type Point2D
  x As Single
  y As Single

  Function Initialize(x As Single, y As Single) 
    Me.x = x
    Me.y = y
  End Function

  Function ToString() As String
    Return StrFormat$("{1},{2}", Me.x, Me.y)
  End Function  
End Type
Type Point3D Extends Point2D
  z As Single

  Function Initialize(x As Single, y As Single, z As Single) 
    Dim base As Point2D At VarPtr(Me) : base.Initialize(x, y)  
    Me.z = z
  End Function

  Function ToString() As String
    Dim base As Point2D At VarPtr(Me)  
    Return StrFormat$("{1},{2}", base.ToString(), Me.z)
  End Function
End Type

Please note while we did not save lines of code in this particular example, the benefit of reusing the base methods increases when solving real world problems with ThinBASIC.

Submit "UDT inheritance: Accessing base type functions" to Facebook Submit "UDT inheritance: Accessing base type functions" to Digg Submit "UDT inheritance: Accessing base type functions" to del.icio.us Submit "UDT inheritance: Accessing base type functions" to StumbleUpon Submit "UDT inheritance: Accessing base type functions" to Google Submit "UDT inheritance: Accessing base type functions" to Twitter

Updated 20-03-2016 at 13:36 by Petr Schreiber

Tags: inheritance, type, udt
Categories
Development

Comments

  1. Petr Schreiber's Avatar
    Syntax updated to reflect what is possible with thinBasic 1.9.16.16+
  2. kryton9's Avatar
    Thanks Petr, that helps explain a lot of the new User Defined Type (UDT) system in thinBasic.