FreeBasic Override Keyword
Last modified October 18, 2026
The FreeBasic Override keyword enables polymorphism by allowing
a derived class to provide a specific implementation of a method that is
already defined in its base class. This is fundamental to object-oriented
programming in FreeBasic.
Basic Definition
In FreeBasic, Override is used in method declarations within
derived classes to indicate that the method replaces an inherited virtual
method from the base class. The method signature must match exactly.
Method overriding enables runtime polymorphism, where the appropriate method version is called based on the actual object type rather than the reference type. This is a key feature of inheritance hierarchies.
Simple Override Example
This basic example demonstrates method overriding with the Override keyword.
Type Animal Extends Object
Declare Virtual Sub MakeSound()
End Type
Type Dog Extends Animal
Declare Sub MakeSound() Override
End Type
Sub Animal.MakeSound()
Print "Animal sound"
End Sub
Sub Dog.MakeSound()
Print "Woof!"
End Sub
Dim As Animal Ptr a = New Dog
a->MakeSound()
Delete a
Here we define a base Animal class with a virtual MakeSound method. The Dog class overrides this method. When called through an Animal pointer, the Dog version executes due to polymorphism. The Override keyword makes this explicit.
Override with Parameters
Overridden methods can accept parameters just like their base versions.
Type Shape Extends Object
Declare Virtual Function Area(w As Integer, h As Integer) As Integer
End Type
Type Rectangle Extends Shape
Declare Function Area(w As Integer, h As Integer) As Integer Override
End Type
Function Shape.Area(w As Integer, h As Integer) As Integer
Return 0
End Function
Function Rectangle.Area(w As Integer, h As Integer) As Integer
Return w * h
End Function
Dim As Shape Ptr s = New Rectangle
Print "Area: "; s->Area(4, 5)
Delete s
The Rectangle class overrides the Area method from Shape with its own implementation. The parameters must match exactly. The Override keyword ensures we're properly overriding a base class method.
Multiple Level Override
Methods can be overridden at multiple levels in an inheritance hierarchy.
Type Vehicle Extends Object
Declare Virtual Sub Start()
End Type
Type Car Extends Vehicle
Declare Virtual Sub Start() Override
End Type
Type ElectricCar Extends Car
Declare Sub Start() Override
End Type
Sub Vehicle.Start()
Print "Starting vehicle"
End Sub
Sub Car.Start()
Print "Starting car engine"
End Sub
Sub ElectricCar.Start()
Print "Starting electric motor"
End Sub
Dim As Vehicle Ptr v = New ElectricCar
v->Start()
Delete v
This shows a three-level inheritance hierarchy where Start is overridden twice. The ElectricCar version is called through a Vehicle pointer, demonstrating polymorphic behavior. Each override must be explicitly marked.
Override with Different Access
Overridden methods can have different access modifiers than their base versions.
Type BaseClass Extends Object
Protected:
Declare Virtual Sub Method()
End Type
Type Derived Extends BaseClass
Public:
Declare Sub Method() Override
End Type
Sub BaseClass.Method()
Print "Base method"
End Sub
Sub Derived.Method()
Print "Derived method"
End Sub
Dim As Derived d
d.Method()
Here the base method is protected while the derived version is public. The Override keyword still applies as the method signatures match. Access modifiers do not affect overriding, only visibility.
Abstract Base with Override
Abstract methods must be overridden in concrete derived classes.
Type AbstractBase Extends Object
Declare Abstract Sub MustImplement()
End Type
Type Concrete Extends AbstractBase
Declare Sub MustImplement() Override
End Type
Sub Concrete.MustImplement()
Print "Implemented in concrete class"
End Sub
Dim As Concrete c
c.MustImplement()
AbstractBase defines an abstract method that Concrete must implement. The Override keyword marks this as an implementation of the abstract method. This is mandatory for abstract methods, unlike regular virtual methods.
Override with Return Types
Overridden methods can have covariant return types in FreeBasic.
Type BaseType Extends Object
End Type
Type DerivedType Extends BaseType
End Type
Type BaseClass Extends Object
Declare Virtual Function Create() As BaseType Ptr
End Type
Type DerivedClass Extends BaseClass
Declare Function Create() As DerivedType Ptr Override
End Type
Function BaseClass.Create() As BaseType Ptr
Return New BaseType
End Function
Function DerivedClass.Create() As DerivedType Ptr
Return New DerivedType
End Function
Dim As BaseClass Ptr b = New DerivedClass
Dim As BaseType Ptr obj = b->Create()
Delete obj
Delete b
This demonstrates covariant return types where the overridden method returns a more specific type. The DerivedClass's Create returns a DerivedType pointer while still satisfying the base method's contract. The Override keyword is still required.
Override vs Overload
This example contrasts overriding with overloading.
Type BaseO Extends Object
Declare Virtual Sub Process(x As Integer)
Declare Virtual Sub Process(s As String)
End Type
Type DerivedO Extends BaseO
Declare Sub Process(x As Integer) Override
Declare Sub Process(s As String) Override
Declare Sub Process(s As String, y As Integer)
End Type
Sub BaseO.Process(x As Integer)
Print "Base Process Integer: "; x
End Sub
Sub BaseO.Process(s As String)
Print "Base Process String: "; s
End Sub
Sub DerivedO.Process(x As Integer)
Print "Derived Process Integer: "; x
End Sub
Sub DerivedO.Process(s As String)
Print "Derived Process String: "; s
End Sub
Sub DerivedO.Process(s As String, y As Integer)
Print "Derived Process String+Integer: "; s; y
End Sub
Dim As DerivedO d
d.Process(10)
d.Process("test")
d.Process("test", 20)
Here Process(Integer) and Process(String) are overridden, while Process(String,Integer) is a new overload. Both overrides require Override as they replace virtual methods. Note that in FreeBASIC a derived type that declares any method with a given name hides the base overloads of that name, so Process(String) must be redeclared in Derived to be callable on a Derived object.
Best Practices
- Explicit Override: Always use Override when intended to make code clearer.
- Signature Matching: Ensure parameter types and return types match exactly.
- Documentation: Document why a method is being overridden.
- Base Calls: Consider calling the base method when appropriate.
- Virtual Destructors: Make destructors virtual when using polymorphism.
This tutorial covered the FreeBasic Override keyword with practical
examples showing its usage in different polymorphism scenarios.
Author
List all FreeBasic Tutorials.