Skip to content

Latest commit

 

History

History
67 lines (47 loc) · 4.75 KB

File metadata and controls

67 lines (47 loc) · 4.75 KB
title Abstract Classes (F#)
description Learn about F# abstract classes, which leave some or all members unimplemented and represent common functionality of a diverse set of object types.
keywords visual f#, f#, functional programming
author cartermp
ms.author phcart
ms.date 05/16/2016
ms.topic language-reference
ms.prod .net
ms.technology devlang-fsharp
ms.devlang fsharp
ms.assetid a3dcc335-433b-4672-ac2d-ae6b11b816f3

Abstract Classes

Abstract classes are classes that leave some or all members unimplemented, so that implementations can be provided by derived classes.

Syntax

// Abstract class syntax.
[<AbstractClass>]
type [ accessibility-modifier ] abstract-class-name =
[ inherit base-class-or-interface-name ]
[ abstract-member-declarations-and-member-definitions ]

// Abstract member syntax.
abstract member member-name : type-signature

Remarks

In object-oriented programming, an abstract class is used as a base class of a hierarchy, and represents common functionality of a diverse set of object types. As the name "abstract" implies, abstract classes often do not correspond directly onto concrete entities in the problem domain. However, they do represent what many different concrete entities have in common.

Abstract classes must have the AbstractClass attribute. They can have implemented and unimplemented members. The use of the term abstract when applied to a class is the same as in other .NET languages; however, the use of the term abstract when applied to methods (and properties) is a little different in F# from its use in other .NET languages. In F#, when a method is marked with the abstract keyword, this indicates that a member has an entry, known as a virtual dispatch slot, in the internal table of virtual functions for that type. In other words, the method is virtual, although the virtual keyword is not used in the F# language. The keyword abstract is used on virtual methods regardless of whether the method is implemented. The declaration of a virtual dispatch slot is separate from the definition of a method for that dispatch slot. Therefore, the F# equivalent of a virtual method declaration and definition in another .NET language is a combination of both an abstract method declaration and a separate definition, with either the default keyword or the override keyword. For more information and examples, see Methods.

A class is considered abstract only if there are abstract methods that are declared but not defined. Therefore, classes that have abstract methods are not necessarily abstract classes. Unless a class has undefined abstract methods, do not use the AbstractClass attribute.

In the previous syntax, accessibility-modifier can be public, private or internal. For more information, see Access Control.

As with other types, abstract classes can have a base class and one or more base interfaces. Each base class or interface appears on a separate line together with the inherit keyword.

The type definition of an abstract class can contain fully defined members, but it can also contain abstract members. The syntax for abstract members is shown separately in the previous syntax. In this syntax, the type signature of a member is a list that contains the parameter types in order and the return types, separated by -> tokens and/or * tokens as appropriate for curried and tupled parameters. The syntax for abstract member type signatures is the same as that used in signature files and that shown by IntelliSense in the Visual Studio Code Editor.

The following code illustrates an abstract class Shape, which has two non-abstract derived classes, Square and Circle. The example shows how to use abstract classes, methods, and properties. In the example, the abstract class Shape represents the common elements of the concrete entities circle and square. The common features of all shapes (in a two-dimensional coordinate system) are abstracted out into the Shape class: the position on the grid, an angle of rotation, and the area and perimeter properties. These can be overridden, except for position, the behavior of which individual shapes cannot change.

The rotation method can be overridden, as in the Circle class, which is rotation invariant because of its symmetry. So in the Circle class, the rotation method is replaced by a method that does nothing.

[!code-fsharpMain]

Output:

Perimeter of square with side length 10.000000 is 40.000000
Circumference of circle with radius 5.000000 is 31.415927
Area of Square: 100.000000
Area of Circle: 78.539816

See Also

Classes

Members

Methods

Properties