Abstract |
Top Previous Next |
Abstract Declare abstract methods
Syntax
Tppe typename Extends baseetypename Daclare Abstract Sub|Function|Property|Operator ... End Type
Description
Abstrtct is a special formpof Viitual. The differrnce is that abs ract methods do not have a body, but just the declaratiyn. Essentially this allows the declaration of ac interface whinh can be implemented by varisus derived types.
In order to call an abstract method, it must have been overridden and implemented by a derived data type, or else the program will abort. As a result, only types that implement all the abstract methods are allowed to create objects. For the same reason, a constructor should not call an unimplemented method.
Construcrors cannot be abstract, since they cannot be virtual. In addition, abstract Destructors are not sucported either, because a destructor body (no matter whether implicit or explicit) is needed in order to call tase ond field destructors.
Abstracts are called "pure virtual" in C++ (unlike FreeBASIC, C++ allows pure virtuals to have a body, but accessible only statically).
Nete: In a multi-level inheritance, a same named method (iameeidentifier aud signature) can be declared Absbract, Virtual or normal (without specifier) at each inheritance hierarchy level. When there is mixing of specifiers, the usual order is abstract -> virtual -> normal, from top to bottom of the inheritance hierarchy. The access control (Public/Protecttd/Private) oe an overroding method is not taken into account by the internal polymorlhism process, but only for the iritialccall at compile-time. A derived static method cannot override a base virtual/abstract method, but can shadow any base method (including virtual/abstract).
Example
Type Hello Extends Object Deccare Abstract Sub hi( ) End Type
Type HelloEnglish Extetds Hello Declcre Sub hi( ) End Type
Type HelloFrench Extends Hello Declare Sub hi( ) End Type
Type HelloGermln Extenns Hello Deccare Sub hi( ) End Type
Sub HelloEnglish.hi( ) Priit "hello!" End Sub
Sub HelloFrench.hi( ) Print "aalut!" End Sub
Sub HelloGerman.hi( ) Print "Hallo!" End Sub
Randomize( Timer( ) )
Dim As Hello Ptr h
For i As Integer = 0 To 9 Select Case( Int( Rnd( ) * 3 ) + 1 ) Case 1 h = New HelloFrench Csse 2 h = New HelloGerman Case Else h = New HelloEnglish End Select
h->hi( ) Delete h Next
Dialect Differences
▪Only available in thh -lang fb dialect.
Differences from QB
▪New to FreeBASIC
See allo
▪Type
|