Extends
 
Specifies a base type from which to derive a new type

Syntax

Type typename Extends base_typename
...
End Type

Description

Extends declares typename to be derived from base_typename. The derived user-defined type, typename, inherits fields and methods of the base_typename base type. typename objects may be used in place of base_typename objects. Fields and methods inherited from base_typename will be implicitly accessible like regular members of typename. However, regular members will override inherited members if they have the same identifier. The Base (Member Access) keyword can be used to explicitly access members of the base type.

User-defined types that extend another type will include the base type structure at their beginning, and their size as reported by Sizeof() is the sum of their base type's size plus the size needed for any regular members. Since the inherited members make sure that the structure is not empty, a derived type is not required to have regular members of its own.

The Base (Initializer) keyword can be used at the top of constructor of derived user-defined type. It allows to specify an initializer or constructor call for the base type.

Extending the built-in Object type allows a user-defined type to be used with Operator Is to perform run-time type checks.

Remark: Derived UDT pointer can only be casted to "compatible" pointer types (up/down the inheritance hierarchy) or Any Ptr. Otherwise, cast to Any Ptr first.

Warning: Before fbc version 0.24, these five keywords dedicated to inheritance Extends, Base (Member Access), Base (Initializer), Object and Operator Is are not supported.

Example

Type SchoolMember 'Represents any school member'
    Declare Constructor ()
    Declare Sub Init (ByRef _name As String, ByVal _age As Integer)
    As String Name
    As Integer age
End Type

Constructor SchoolMember ()
    Print "Initialized SchoolMember"
End Constructor

Sub SchoolMember.Init (ByRef _name As String, ByVal _age As Integer)
    This.name = _name
    This.age = _age
    Print "Name: "; This.name; "   Age:"; This.age
End Sub


Type Teacher Extends SchoolMember 'Represents a teacher derived from SchoolMember'
    Declare Constructor (ByRef _name As String, ByVal _age As Integer, ByVal _salary As Integer)
    As Integer salary
    Declare Sub Tell ()
End Type

Constructor Teacher (ByRef _name As String, ByVal _age As Integer, ByVal _salary As Integer)
    Print "Initialized Teacher"
    This.Init(_name, _age) 'implicit access to base member procedure'
    This.salary = _salary
End Constructor

Sub Teacher.Tell ()
    Print "Salary:"; This.salary
End Sub


Type Student Extends SchoolMember 'Represents a student derived from SchoolMember'
    Declare Constructor (ByRef _name As String, ByVal _age As Integer, ByVal _marks As Integer)
    As Integer marks
    Declare Sub Tell ()
End Type

Constructor Student (ByRef _name As String, ByVal _age As Integer, ByVal _marks As Integer)
    Print "Initialized Student"
    This.Init(_name, _age) 'implicit access to base member procedure'
    This.marks = _marks
End Constructor
    
Sub Student.Tell ()
    Print "Marks:"; This.marks
End Sub


Dim As Teacher t = Teacher("Mrs. Shrividya", 40, 30000)
t.Tell()
Print
Dim As Student s = Student("Swaroop", 22, 75)
s.Tell()

Dialect Differences

  • Not available in the -lang qb dialect unless referenced with the alias __Extends.

Differences from QB

  • New to FreeBASIC

See also