FreeBasic Abstract Keyword
Last modified October 18, 2026
The FreeBasic Abstract keyword is used to create abstract classes
and methods that cannot be instantiated directly. Abstract classes serve as
base classes for inheritance hierarchies.
Basic Definition
In FreeBasic, Abstract is a method modifier used with
Declare. A type that contains abstract methods must extend
Object (or a type derived from it). The Abstract
keyword cannot be applied to the Type declaration itself.
Types with unimplemented abstract methods cannot be instantiated directly;
attempting to do so produces a compile-time error.
Abstract methods have no body in the base type. Derived types must provide concrete implementations for all abstract methods before objects of them can be created. This enables polymorphism and enforces a consistent interface.
Simple Abstract Class
This example demonstrates the basic structure of an abstract class.
Type Animal Extends Object
Declare Abstract Sub MakeSound()
End Type
Type Dog Extends Animal
Declare Sub MakeSound()
End Type
Sub Dog.MakeSound()
Print "Woof!"
End Sub
Dim As Dog Ptr myDog = New Dog
myDog->MakeSound()
Delete myDog
Here we define an abstract Animal type with one abstract method.
The Dog type inherits from Animal and implements
the MakeSound method. The output is:
Woof!
We cannot create an instance of Animal; doing so while
MakeSound is unimplemented produces a compile-time error.
Abstract Class with Concrete Methods
Abstract classes can contain both abstract and implemented methods.
Type Shape Extends Object
Declare Abstract Function Area() As Single
Declare Sub Display()
End Type
Sub Shape.Display()
Print "This is a shape"
End Sub
Type Circle Extends Shape
radius As Single
Declare Function Area() As Single
End Type
Function Circle.Area() As Single
Return 3.14159 * radius * radius
End Function
Dim As Circle Ptr c = New Circle
c->radius = 5
c->Display()
Print "Area: "; c->Area()
Delete c
The Shape type has one abstract method and one concrete method.
Circle implements the abstract Area method while
inheriting the Display method. This shows how abstract types
can provide partial implementations. The output is:
This is a shape Area: 78.53975
Multiple Abstract Methods
Abstract classes can define multiple methods that must be implemented.
Type Vehicle Extends Object
Declare Abstract Sub Start()
Declare Abstract Sub Stop()
Declare Abstract Function GetSpeed() As Integer
End Type
Type Car Extends Vehicle
speed As Integer
Declare Sub Start()
Declare Sub Stop()
Declare Function GetSpeed() As Integer
End Type
Sub Car.Start()
Print "Car started"
speed = 0
End Sub
Sub Car.Stop()
Print "Car stopped"
speed = 0
End Sub
Function Car.GetSpeed() As Integer
Return speed
End Function
Dim As Car Ptr myCar = New Car
myCar->Start()
Print "Speed: "; myCar->GetSpeed()
myCar->Stop()
Delete myCar
The Vehicle abstract type defines three abstract methods.
The Car type provides implementations for all of them.
Any type inheriting from Vehicle must implement these methods
before objects of it can be created. The output is:
Car started Speed: 0 Car stopped
Abstract Properties
FreeBasic allows abstract properties in abstract classes.
Type Person Extends Object
Declare Abstract Property Name() As String
End Type
Type Student Extends Person
privateName As String
Declare Property Name() As String
Declare Property Name(ByVal n As String)
End Type
Property Student.Name() As String
Return privateName
End Property
Property Student.Name(ByVal n As String)
privateName = n
End Property
Dim As Student Ptr s = New Student
s->Name = "Alice"
Print "Student name: "; s->Name
Delete s
The output is:
Student name: Alice
This example shows an abstract property in the Person type.
The Student class implements both getter and setter for the
property. Abstract properties enforce a consistent interface across derived
classes.
Abstract Class Inheritance
Abstract classes can inherit from other abstract classes.
Type Database Extends Object
Declare Abstract Sub Connect()
End Type
Type SqlDatabase Extends Database
Declare Abstract Sub ExecuteQuery(query As String)
End Type
Type MySqlDatabase Extends SqlDatabase
Declare Sub Connect()
Declare Sub ExecuteQuery(query As String)
End Type
Sub MySqlDatabase.Connect()
Print "Connected to MySQL"
End Sub
Sub MySqlDatabase.ExecuteQuery(query As String)
Print "Executing: "; query
End Sub
Dim As MySqlDatabase Ptr db = New MySqlDatabase
db->Connect()
db->ExecuteQuery("SELECT * FROM users")
Delete db
The output is:
Connected to MySQL Executing: SELECT * FROM users
Here we have a hierarchy of abstract types. SqlDatabase inherits
from Database and adds another abstract method. The concrete
MySqlDatabase implements all inherited abstract methods.
Interface-like Abstract Class
Abstract classes can be used to create interface-like constructs.
Type Drawable Extends Object
Declare Abstract Sub Draw()
End Type
Type Circle Extends Drawable
Declare Sub Draw()
End Type
Sub Circle.Draw()
Print "Drawing a circle"
End Sub
Type Square Extends Drawable
Declare Sub Draw()
End Type
Sub Square.Draw()
Print "Drawing a square"
End Sub
Dim As Drawable Ptr shapes(0 To 1)
shapes(0) = New Circle
shapes(1) = New Square
For i As Integer = 0 To 1
shapes(i)->Draw()
Delete shapes(i)
Next
This example demonstrates polymorphism using an abstract type as an interface.
Both Circle and Square implement the Draw
method. We can store different types in an array of the base abstract type.
The output is:
Drawing a circle Drawing a square
Abstract Class with Constructor
Abstract classes can have constructors, which are called by derived classes.
Type Logger Extends Object
Protected:
logLevel As Integer
Public:
Declare Constructor(level As Integer = 0)
Declare Abstract Sub Log(message As String)
End Type
Constructor Logger(level As Integer)
logLevel = level
End Constructor
Type FileLogger Extends Logger
Public:
Declare Constructor(level As Integer = 0)
Declare Sub Log(message As String)
End Type
Constructor FileLogger(level As Integer)
Base.Constructor(level)
End Constructor
Sub FileLogger.Log(message As String)
Print "["; logLevel; "] "; message
End Sub
Dim As FileLogger Ptr lg = New FileLogger(2)
lg->Log("System started")
Delete lg
The Logger abstract type has a constructor that initializes
the log level. The FileLogger type calls the base constructor
and implements the abstract Log method. The default argument on
the constructors is needed here, because FreeBASIC otherwise requires an
explicit default constructor for a derived type whose base has no default
constructor. The output is:
[ 2] System started
Best Practices
- Naming: Use descriptive names for abstract classes that indicate their purpose.
- Documentation: Clearly document the expected behavior of abstract methods.
- Minimalism: Keep abstract classes focused on a single responsibility.
- Implementation: Ensure all abstract methods are implemented in derived classes.
- Design: Use abstract classes to define common interfaces for related classes.
This tutorial covered the FreeBasic Abstract keyword with practical
examples showing its usage in different scenarios. Abstract classes are powerful
tools for creating flexible and maintainable object-oriented designs.
Author
List all FreeBasic Tutorials.