ZetCode

FreeBasic Override Keyword

last modified June 16, 2025

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.

override_simple.bas
Type Animal
    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.

override_parameters.bas
Type Shape
    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) 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.

override_multilevel.bas
Type Vehicle
    Declare Virtual Sub Start()
End Type

Type Car Extends Vehicle
    Declare 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.

override_access.bas
Type Base
    Declare Virtual Protected Sub Method()
End Type

Type Derived Extends Base
    Declare Sub Method() Override
End Type

Sub Base.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 don't affect overriding, only visibility.

Abstract Base with Override

Abstract methods must be overridden in concrete derived classes.

override_abstract.bas
Type AbstractBase
    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.

override_covariant.bas
Type BaseType
End Type

Type DerivedType Extends BaseType
End Type

Type BaseClass
    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.

override_vs_overload.bas
Type Base
    Declare Virtual Sub Process(x As Integer)
    Declare Sub Process(s As String)
End Type

Type Derived Extends Base
    Declare Sub Process(x As Integer) Override
    Declare Sub Process(s As String, y As Integer)
End Type

Sub Base.Process(x As Integer)
    Print "Base Process Integer: "; x
End Sub

Sub Base.Process(s As String)
    Print "Base Process String: "; s
End Sub

Sub Derived.Process(x As Integer)
    Print "Derived Process Integer: "; x
End Sub

Sub Derived.Process(s As String, y As Integer)
    Print "Derived Process String+Integer: "; s; y
End Sub

Dim As Derived d
d.Process(10)
d.Process("test")
d.Process("test", 20)

Here Process(Integer) is overridden while Process(String) is inherited and Process(String,Integer) is overloaded. Only the first requires Override as it replaces a virtual method. Overloading adds new methods with different signatures.

Best Practices

This tutorial covered the FreeBasic Override keyword with practical examples showing its usage in different polymorphism scenarios.

Author

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.