Inheritance and abstract classes

This is a follow-on from the previous post on classes. This post will focus on inheritance in F#, and how to define and use abstract classes and interfaces.

Inheritance

To declare that a class inherits from another class, use the syntax:

type DerivedClass(param1, param2) =
   inherit BaseClass(param1)

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.

public class MyBaseClass
{
    public MyBaseClass(int param1)
    {
        this.Param1 = param1;
    }
    public int Param1 { get; private set; }
}

public class MyDerivedClass: MyBaseClass
{
    public MyDerivedClass(int param1,int param2): base(param1)
    {
        this.Param2 = param2;
    }
    public int Param2 { get; private set; }
}

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.

Abstract and virtual methods

Obviously, part of the point of inheritance is to be able to have abstract methods, virtual methods, and so on.

Defining abstract methods in the base class

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.

Defining abstract properties

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.

Default implementations (but no virtual methods)

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.

Abstract classes

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.

Overriding methods in subclasses

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#.

Summary of abstract methods

Abstract methods are basically straightforward and similar to C#. There are only two areas that might be tricky if you are used to C#:

  • You must understand how function signatures work and what their syntax is! For a detailed discussion see the post on function signatures.

  • There is no all-in-one virtual method. You must define the abstract method and the default implementation separately.

Last updated

Was this helpful?