Last modified April 20, 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.
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 or override inherited ones.
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.
This example demonstrates basic inheritance using the Extends keyword.
Type Animal
Public:
name As String
Declare Constructor(name As String)
Declare Sub MakeSound()
End Type
Constructor Animal(name As String)
This.name = name
End Constructor
Sub Animal.MakeSound()
Print "Some generic animal sound"
End Sub
Type Dog Extends Animal
Public:
Declare Constructor(name As String)
Declare Sub MakeSound()
End Type
Constructor Dog(name As String)
Base(name)
End Constructor
Sub Dog.MakeSound()
Print name; " says: Woof!"
End Sub
Dim d As Dog = 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. This demonstrates polymorphism in action.
Derived types can access base class members using the Base keyword.
Type Vehicle
Public:
speed As Integer
Declare Constructor(speed As Integer)
Declare Sub Display()
End Type
Constructor Vehicle(speed As Integer)
This.speed = speed
End Constructor
Sub Vehicle.Display()
Print "Speed: "; speed; " km/h"
End Sub
Type Car Extends Vehicle
Public:
model As String
Declare Constructor(model As String, speed As Integer)
Declare Sub Display()
End Type
Constructor Car(model As String, speed As Integer)
Base(speed)
This.model = model
End Constructor
Sub Car.Display()
Base.Display()
Print "Model: "; model
End Sub
Dim c As Car = 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.
FreeBasic supports multi-level inheritance chains.
Type Person
Public:
name As String
Declare Constructor(name As String)
End Type
Constructor Person(name As String)
This.name = name
End Constructor
Type Employee Extends Person
Public:
id As Integer
Declare Constructor(name As String, id As Integer)
End Type
Constructor Employee(name As String, id As Integer)
Base(name)
This.id = id
End Constructor
Type Manager Extends Employee
Public:
department As String
Declare Constructor(name As String, id As Integer, department As String)
Declare Sub Display()
End Type
Constructor Manager(name As String, id As Integer, department As String)
Base(name, id)
This.department = department
End Constructor
Sub Manager.Display()
Print "Name: "; name
Print "ID: "; id
Print "Department: "; department
End Sub
Dim m As Manager = 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.
Derived types can override base type methods to provide specialized behavior.
Type Shape Public: Declare Function Area() As Double End Type Function Shape.Area() As Double Return 0 End Function Type Circle Extends Shape Public: radius As Double Declare Constructor(radius As Double) Declare Function Area() As Double End Type Constructor Circle(radius As Double) This.radius = radius End Constructor Function Circle.Area() As Double Return 3.14159 * radius * radius End Function Type Square Extends Shape Public: side As Double Declare Constructor(side As Double) Declare Function Area() As Double End Type Constructor Square(side As Double) This.side = side End Constructor Function Square.Area() As Double Return side * 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. The array of Shape pointers demonstrates polymorphism - calling Area() invokes the appropriate derived class implementation.
FreeBasic can simulate abstract base classes using pure virtual methods.
Type Animal Abstract Public: Declare Abstract Sub Speak() End Type Type Cat Extends Animal Public: Declare Sub Speak() End Type Sub Cat.Speak() Print "Meow" End Sub Type Dog Extends Animal Public: 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
The Animal type is marked Abstract with an Abstract Speak() method. Derived types must implement Speak(). This enforces an interface that all animals must follow. The Abstract keyword prevents instantiation of the base class.
FreeBasic supports multiple inheritance through abstract interfaces.
Type IPrintable Abstract Public: Declare Abstract Sub Print() End Type Type IScannable Abstract Public: Declare Abstract Sub Scan() End Type Type Printer Public: Declare Sub Print() End Type Sub Printer.Print() Print "Printing document..." End Sub Type Scanner Public: Declare Sub Scan() End Type Sub Scanner.Scan() Print "Scanning document..." End Sub Type MultiFunctionDevice Extends Printer, Scanner Public: Declare Sub Fax() End Type Sub MultiFunctionDevice.Fax() Print "Sending fax..." End Sub Dim mfd As MultiFunctionDevice mfd.Print() mfd.Scan() mfd.Fax()
This demonstrates multiple inheritance. MultiFunctionDevice inherits from both Printer and Scanner. Each parent type implements a different interface. The derived type combines all capabilities and adds new ones.
Constructors in inheritance hierarchies must properly chain to base constructors.
Type BaseType Public: value As Integer Declare Constructor(v As Integer) End Type Constructor BaseType(v As Integer) value = v Print "BaseType constructor: "; value End Constructor Type DerivedType Extends BaseType Public: text As String Declare Constructor(v As Integer, t As String) End Type Constructor DerivedType(v As Integer, t As String) Base(v) text = t Print "DerivedType constructor: "; text End Constructor Dim d As DerivedType = DerivedType(42, "Hello") Print "Final object: "; d.value; ", "; d.text
The DerivedType constructor must call the BaseType constructor using Base(). Constructors are called from the base up to the most derived type. Fields are initialized in the same order. Proper constructor chaining ensures complete object initialization.
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.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all FreeBasic Tutorials.