ZetCode

FreeBasic Abstract Keyword

Last modified April 20, 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 marks a class or method that must be implemented by derived classes. Abstract classes cannot be instantiated directly and are meant to be inherited from.

Abstract methods have no implementation in the base class. Derived classes must provide concrete implementations for all abstract methods. This enables polymorphism and enforces a consistent interface.

Simple Abstract Class

This example demonstrates the basic structure of an abstract class.

abstract_simple.bas
Type Animal Abstract
 Declare Abstract Sub MakeSound()
End Type
Type Dog Extends Animal
 Declare Sub MakeSound()
 Print "Woof!"
 End Sub
End Type
Dim As Dog Ptr myDog = New Dog
myDog->MakeSound()
Delete myDog

Here we define an abstract Animal class with one abstract method. The Dog class inherits from Animal and implements the MakeSound method. We cannot create an instance of Animal.

Abstract Class with Concrete Methods

Abstract classes can contain both abstract and implemented methods.

abstract_mixed.bas
Type Shape Abstract
 Declare Abstract Function Area() As Single
 Declare Sub Display()
 Print "This is a shape"
 End Sub
End Type
Type Circle Extends Shape
 radius As Single
 Declare Function Area() As Single
 Return 3.14159 * radius * radius
 End Sub
End Type
Dim As Circle Ptr c = New Circle
c->radius = 5
c->Display()
Print "Area: "; c->Area()
Delete c

The Shape class has one abstract method and one concrete method. Circle implements the abstract Area method while inheriting the Display method. This shows how abstract classes can provide partial implementations.

Multiple Abstract Methods

Abstract classes can define multiple methods that must be implemented.

abstract_multiple.bas
Type Vehicle Abstract
 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()
 Print "Car started"
 speed = 0
 End Sub
 Declare Sub Stop()
 Print "Car stopped"
 speed = 0
 End Sub
 Declare Function GetSpeed() As Integer
 Return speed
 End Sub
End Type
Dim As Car Ptr myCar = New Car
myCar->Start()
Print "Speed: "; myCar->GetSpeed()
myCar->Stop()
Delete myCar

The Vehicle abstract class defines three abstract methods. The Car class provides implementations for all of them. Any class inheriting from Vehicle must implement these methods.

Abstract Properties

FreeBasic allows abstract properties in abstract classes.

abstract_property.bas
Type Person Abstract
 Declare Abstract Property Name() As String
End Type
Type Student Extends Person
 privateName As String
 Declare Property Name() As String
 Return privateName
 End Property
 Declare Property Name(ByVal n As String)
 privateName = n
 End Property
End Type
Dim As Student Ptr s = New Student
s->Name = "Alice"
Print "Student name: "; s->Name
Delete s

This example shows an abstract property in the Person class. 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.

abstract_inheritance.bas
Type Database Abstract
 Declare Abstract Sub Connect()
End Type
Type SqlDatabase Abstract Extends Database
 Declare Abstract Sub ExecuteQuery(query As String)
End Type
Type MySqlDatabase Extends SqlDatabase
 Declare Sub Connect()
 Print "Connected to MySQL"
 End Sub
 Declare Sub ExecuteQuery(query As String)
 Print "Executing: "; query
 End Sub
End Type
Dim As MySqlDatabase Ptr db = New MySqlDatabase
db->Connect()
db->ExecuteQuery("SELECT * FROM users")
Delete db

Here we have a hierarchy of abstract classes. 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.

abstract_interface.bas
Type Drawable Abstract
 Declare Abstract Sub Draw()
End Type
Type Circle Extends Drawable
 Declare Sub Draw()
 Print "Drawing a circle"
 End Sub
End Type
Type Square Extends Drawable
 Declare Sub Draw()
 Print "Drawing a square"
 End Sub
End Type
Dim As Drawable Ptr shapes(0 To 1) = {New Circle, New Square}
For i As Integer = 0 To 1
 shapes[i]->Draw()
 Delete shapes[i]
Next

This example demonstrates polymorphism using an abstract class as an interface. Both Circle and Square implement the Draw method. We can store different types in an array of the base abstract type.

Abstract Class with Constructor

Abstract classes can have constructors, which are called by derived classes.

abstract_constructor.bas
Type Logger Abstract
 protected:
 logLevel As Integer
 public:
 Declare Constructor(level As Integer)
 logLevel = level
 End Constructor
 Declare Abstract Sub Log(message As String)
End Type
Type FileLogger Extends Logger
 Declare Constructor(level As Integer)
 Base.Constructor(level)
 End Constructor
 Declare Sub Log(message As String)
 Print "["; logLevel; "] "; message
 End Sub
End Type
Dim As FileLogger Ptr logger = New FileLogger(2)
logger->Log("System started")
Delete logger

The Logger abstract class has a constructor that initializes the log level. The FileLogger class calls the base constructor and implements the abstract Log method. Abstract class constructors help initialize common state.

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

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.

AltStyle によって変換されたページ (->オリジナル) /