FreeBasic Extends Keyword
Last modified October 18, 2026
The FreeBasic Extends keyword enables inheritance in object-oriented
programming. It allows a derived type to inherit members from a base type.
Inheritance promotes code reuse and hierarchical relationships between types.
Basic Definition
In FreeBasic, Extends creates an inheritance relationship between
types. The derived type inherits all fields and methods from the base type.
The derived type can add new members. A local member with the same name shadows
the inherited one; the Base keyword gives access to the shadowed base member.
True virtual overriding requires a base type that extends Object and methods
declared Virtual, with Override on the derived declaration.
Inheritance follows an "is-a" relationship. For example, a Dog extends Animal because a dog is an animal. The base type is more general, while derived types are more specific.
Simple Inheritance
This example demonstrates basic inheritance using the Extends keyword.
Type Animal
Declare Constructor (ByRef _name As String = "")
Declare Sub MakeSound ()
As String name
End Type
Constructor Animal (ByRef _name As String)
This.name = _name
End Constructor
Sub Animal.MakeSound ()
Print "Some generic animal sound"
End Sub
Type Dog Extends Animal
Declare Constructor (ByRef _name As String = "")
Declare Sub MakeSound ()
End Type
Constructor Dog (ByRef _name As String)
Base(_name)
End Constructor
Sub Dog.MakeSound ()
Print This.name; " says: Woof!"
End Sub
Dim As Dog d = Dog("Rex")
d.MakeSound()
Here, Dog extends Animal, inheriting its name field. The Dog constructor calls
the base constructor using Base(). Dog overrides MakeSound() with its own
implementation. Output: Rex says: Woof!. Note that this is
static (non-virtual) overriding; calls through a base-type reference or pointer
would not dispatch to Dog.MakeSound() unless the method is declared Virtual.
Accessing Base Class Members
Derived types can access base class members using the Base keyword.
Type Vehicle
Declare Constructor (ByVal _speed As Integer = 0)
Declare Sub Display ()
As Integer speed
End Type
Constructor Vehicle (ByVal _speed As Integer)
This.speed = _speed
End Constructor
Sub Vehicle.Display ()
Print "Speed: "; This.speed; " km/h"
End Sub
Type Car Extends Vehicle
Declare Constructor (ByRef _model As String, ByVal _speed As Integer)
Declare Sub Display ()
As String model
End Type
Constructor Car (ByRef _model As String, ByVal _speed As Integer)
Base(_speed)
This.model = _model
End Constructor
Sub Car.Display ()
Base.Display()
Print "Model: "; This.model
End Sub
Dim As Car c = Car("Toyota", 120)
c.Display()
The Car type extends Vehicle and adds a model field. Its Display() method calls
the base class Display() before adding its own output. Base allows accessing
the immediate parent's members. Output: Speed: 120 km/h followed
by Model: Toyota.
Multi-level Inheritance
FreeBasic supports multi-level inheritance chains.
Type Person
Declare Constructor (ByRef _name As String = "")
As String name
End Type
Constructor Person (ByRef _name As String)
This.name = _name
End Constructor
Type Employee Extends Person
Declare Constructor (ByRef _name As String = "", ByVal _id As Integer = 0)
As Integer id
End Type
Constructor Employee (ByRef _name As String, ByVal _id As Integer)
Base(_name)
This.id = _id
End Constructor
Type Manager Extends Employee
Declare Constructor (ByRef _name As String, ByVal _id As Integer, ByRef _department As String)
Declare Sub Display ()
As String department
End Type
Constructor Manager (ByRef _name As String, ByVal _id As Integer, ByRef _department As String)
Base(_name, _id)
This.department = _department
End Constructor
Sub Manager.Display ()
Print "Name: "; This.name
Print "ID: "; This.id
Print "Department: "; This.department
End Sub
Dim As Manager m = Manager("Alice", 1001, "Engineering")
m.Display()
This shows a three-level inheritance hierarchy: Manager extends Employee, which extends Person. Each derived type adds new fields while inheriting all previous ones. The Manager constructor chains through both parent constructors.
Overriding Methods
Derived types can override base type methods to provide specialized behavior.
Type Shape Extends Object
Declare Virtual Function Area () As Double
End Type
Function Shape.Area () As Double
Return 0
End Function
Type Circle Extends Shape
Declare Constructor (ByVal _radius As Double)
Declare Function Area () As Double Override
As Double radius
End Type
Constructor Circle (ByVal _radius As Double)
This.radius = _radius
End Constructor
Function Circle.Area () As Double
Return 3.14159 * This.radius * This.radius
End Function
Type Square Extends Shape
Declare Constructor (ByVal _side As Double)
Declare Function Area () As Double Override
As Double side
End Type
Constructor Square (ByVal _side As Double)
This.side = _side
End Constructor
Function Square.Area () As Double
Return This.side * This.side
End Function
Dim shapes(1 To 2) As Shape Ptr
shapes(1) = New Circle(5.0)
shapes(2) = New Square(4.0)
For i As Integer = 1 To 2
Print "Area: "; shapes(i)->Area()
Delete shapes(i)
Next
Shape defines a generic Area() method. Circle and Square override it with their
specific implementations. Because the base method is declared Virtual (which
requires the base type to extend Object), calling Area() through a Shape pointer
invokes the appropriate derived class implementation. Output:
Area: 78.53975 and Area: 16.
Abstract Base Class
FreeBasic can simulate abstract base classes using Abstract methods on a type that extends Object.
Type Animal Extends Object
Declare Abstract Sub Speak ()
End Type
Type Cat Extends Animal
Declare Sub Speak ()
End Type
Sub Cat.Speak ()
Print "Meow"
End Sub
Type Dog Extends Animal
Declare Sub Speak ()
End Type
Sub Dog.Speak ()
Print "Woof"
End Sub
Dim animals(1 To 2) As Animal Ptr
animals(1) = New Cat()
animals(2) = New Dog()
For i As Integer = 1 To 2
animals(i)->Speak()
Delete animals(i)
Next
Animal extends the built-in Object type, and its Speak() method is declared
Abstract, meaning it has no body and must be overridden by derived types.
Derived types must implement Speak(). The Abstract attribute requires
Extends Object; the type itself is not declared with an
"Abstract" keyword. Output: Meow and Woof.
Composition Instead of Multiple Inheritance
FreeBasic does not support multiple inheritance: a type can have only one
Extends base. Composition is used instead to combine capabilities.
Type Printer
Declare Sub PrintDoc ()
As Integer pages
End Type
Sub Printer.PrintDoc ()
Print "Printing document..."
End Sub
Type Scanner
Declare Sub ScanDoc ()
As Integer dpi
End Type
Sub Scanner.ScanDoc ()
Print "Scanning document..."
End Sub
Type MultiFunctionDevice Extends Printer
Declare Sub Fax ()
As Scanner scanner
End Type
Sub MultiFunctionDevice.Fax ()
Print "Sending fax..."
End Sub
Dim As MultiFunctionDevice mfd
mfd.PrintDoc()
mfd.scanner.ScanDoc()
mfd.Fax()
MultiFunctionDevice extends Printer and contains a Scanner as a field, so it
gains printing directly through inheritance and scanning through composition.
Output: Printing document..., Scanning document...,
Sending fax....
Constructor Chaining
Constructors in inheritance hierarchies must properly chain to base constructors.
Type BaseType
Declare Constructor (ByVal _v As Integer = 0)
As Integer value
End Type
Constructor BaseType (ByVal _v As Integer)
This.value = _v
Print "BaseType constructor: "; This.value
End Constructor
Type DerivedType Extends BaseType
Declare Constructor (ByVal _v As Integer, ByRef _t As String)
As String text
End Type
Constructor DerivedType (ByVal _v As Integer, ByRef _t As String)
Base(_v)
This.text = _t
Print "DerivedType constructor: "; This.text
End Constructor
Dim As DerivedType d = DerivedType(42, "Hello")
Print "Final object: "; d.value; ", "; d.text
The DerivedType constructor must call the BaseType constructor using Base().
The base constructor runs first, then the derived constructor body. Output:
BaseType constructor: 42, DerivedType constructor: Hello,
Final object: 42, Hello.
Best Practices
- Design: Use inheritance only for true "is-a" relationships.
- Depth: Limit inheritance depth to 2-3 levels for clarity.
- Composition: Prefer composition over deep inheritance.
- Abstract: Use abstract base classes to define interfaces.
- Virtual: Mark methods as virtual when they should be overridable.
This tutorial covered the FreeBasic Extends keyword with practical
examples showing inheritance in different scenarios. Proper use of inheritance
leads to cleaner, more maintainable code.
Author
List all FreeBasic Tutorials.