F Sharp Interfaces - F Sharp (programming language)

What is F# - Interfaces?

Interfaces provide an abstract way of writing up the implementation details of a class. It is a template that declares the methods the class must implement and expose publicly.

Syntax

An interface specifies the sets of related members that other classes implement. It has the following syntax −

Please note −

  • In an interface declaration the members are not implemented.
  • The members are abstract, declared by the abstract keyword. However you may provide a default implementation using the default keyword.
  • You can implement interfaces either by using object expressions or by using class types.
  • In class or object implementation, you need to provide method bodies for abstract methods of the interface.
  • The keywords interface and end, which mark the start and end of the definition, are optional.

For example,

Calling Interface Methods

Interface methods are called through the interface, not through the instance of the class or type implementing interface. To call an interface method, you up cast to the interface type by using the :> operator (upcast operator).

For example,

The following example illustrates the concept −

Example

On compiling and running, we get the below output

Interface Inheritance

Interfaces can inherit from one or more base interfaces.

The following example shows the concept −

On compiling and running, we get the below output

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

F Sharp (programming language) Topics