Inheritance and abstract classes
Last updated
Was this helpful?
Last updated
Was this helpful?
This is a follow-on from the . This post will focus on inheritance in F#, and how to define and use abstract classes and interfaces.
To declare that a class inherits from another class, use the syntax:
The inherit
keyword signals that DerivedClass
inherits from BaseClass
. In addition, some BaseClass
constructor must be called at the same time.
It might be useful to compare F# with C# at this point. Here is some C# code for a very simple pair of classes.
Note that the inheritance declaration class MyDerivedClass: MyBaseClass
is distinct from the constructor which calls base(param1)
.
Now here is the F# version:
Unlike C#, the inheritance part of the declaration, inherit BaseClass(param1)
, contains both the class to inherit from and its constructor.
Obviously, part of the point of inheritance is to be able to have abstract methods, virtual methods, and so on.
In C#, an abstract method is indicated by the abstract
keyword plus the method signature. In F#, it is the same concept, except that the way that function signatures are written in F# is quite different from C#.
So to define an abstract method, we use the signature syntax, along with the abstract member
keywords:
Notice that the equals sign has been replaced with a colon. This is what you would expect, as the equals sign is used for binding values, while the colon is used for type annotation.
Now, if you try to compile the code above, you will get an error! The compiler will complain that there is no implementation for the method. To fix this, you need to:
provide a default implementation of the method, or
tell the compiler that the class as whole is also abstract.
We'll look at both of these alternatives shortly.
An abstract immutable property is defined in a similar way. The signature is just like that of a simple value.
If the abstract property is read/write, you add the get/set keywords.
To provide a default implementation of an abstract method in the base class, use the default
keyword instead of the member
keyword:
You can see that the default method is defined in the usual way, except for the use of default
instead of member
.
One major difference between F# and C# is that in C# you can combine the abstract definition and the default implementation into a single method, using the virtual
keyword. In F#, you cannot. You must declare the abstract method and the default implementation separately. The abstract member
has the signature, and the default
has the implementation.
If at least one abstract method does not have a default implementation, then the entire class is abstract, and you must indicate this by annotating it with the AbstractClass
attribute.
If this is done, then the compiler will no longer complain about a missing implementation.
To override an abstract method or property in a subclass, use the override
keyword instead of the member
keyword. Other than that change, the overridden method is defined in the usual way.
And to call a base method, use the base
keyword, just as in C#.
Abstract methods are basically straightforward and similar to C#. There are only two areas that might be tricky if you are used to C#:
There is no all-in-one virtual method. You must define the abstract method and the default implementation separately.
You must understand how function signatures work and what their syntax is! For a detailed discussion see the .