Interfaces

An interface determines a contract. An interface itself does not implement members but is used to describe signature of interface members. A class or structure that implement an interface must adhere to its contract and contain implementation of all members described in the interface. An interface may inherit from multiple basic interfaces, and a class or structure may implement multiple interfaces.

An interface declaration consists of an optional set of interface attributes, followed by the optional Public Friend access modifiers, followed by the Interface keyword and an interface name. Then may follow parent interfaces via a colon. Then follows an interface body ended with a sequence of End Interface keywords.

Interface members are sometimes referred to by their fully qualified name. The fully qualified name of an interface member consists of the interface name, followed by a dot, followed by the member name. If an interface is part of a namespace, the fully qualified name of an interface member includes the namespace name. Access modifiers for such members are not specified. The following modifiers cannot be used to declare such members: Abstract, Virtual, Override and Shared.

Interface Body

An interface body can describe signature of the following members:

Below are various examples of interface description and implementation.

Interface IUserInterface
    Sub Test1();
    Sub Test2();
End Interface;

Abstract Class BaseAbstractClass: IUserInterface
    Public Sub Test1();
    Begin
        //Interface method implementation
    End Sub;
    
    Public Sub Test2();
    Begin
        //Interface method implementation
        Test3();
    End Sub;

    Abstract Public Sub Test3();
End Class;

Class UserObject: BaseAbstractClass
    Public Override Sub Test3();
    Begin
        //Abstract method implementation
    End Sub;
End Class;

Sub Test();
Var
    Obj: UserObject = New UserObject();
Begin
    Obj.Test1(); //Invocation of method implemented in abstract class
    Obj.Test2(); //Invocation of method implemented in abstract class
    Obj.Test3(); //Invocation of abstract method implemented in the Object class
End Sub;

Interface IUserStruct
    Property Title: string
        Get;
        Set;
    End Property;
End Interface;

Struct UserStructure: IUserStruct
    s: string;
    Public Param1: integer;
    Public Param2: double;
    
    Public Property Title: string
        Get
        Begin
            Return s;
        End Get
        Set
        Begin
            s := Value
        End Set
    End Property;

    Public Constructor Create(Title: string; Param1Value: integer; Param2Value: double);
    Begin
        s := Title;
        Param1 := Param1Value;
        Param2 := Param2Value;
    End Constructor;
End Struct;

Sub Test();
Var
    StructObj: UserStructure;
Begin
    StructObj := New UserStructure("User structure"1003.14);
End Sub;

Interface IUserInterface1
    Sub Test1();
    Sub Test2();
End Interface;

Interface IUserInterface2
    Sub Test2();
End Interface;

//Interface implementation
Class BaseClass: IUserInterface1, IUserInterface2
    Public Sub Test1();
    Begin
    End Sub;
    //Common method for two interfaces
    Public Sub Test2();
    Begin
    End Sub;
End Class;

//Repeated implementation of the IUserInterface1 interface
Class ChildClass: BaseClass, IUserInterface2
    //Explicit implementation of the IUserInterface2.Test2 method
    Sub IUserInterface2.Test2();
    Begin
    End Sub;
End Class;

Sub Test();
Var
    Obj: BaseClass = New BaseClass();
    Obj1: ChildClass = New ChildClass();
Begin
    Obj.Test1(); //Invocation of the BaseClass.Test1 method
    (Obj As IUserInterface1).Test2(); //Invocation of the  BaseClass.Test2 method
    (Obj As IUserInterface2).Test2(); //Invocation of the BaseClass.Test2 method
    
    Obj1.Test1(); //Invocation of the BaseClass.Test1 method
    Obj1.Test2(); //Invocation of the BaseClass.Test2 method
    (Obj1 As IUserInterface2).Test2(); //Invocation of the ChildClass.Test2 method
End Sub;

See also:

Fore.NET Language Guide