interface

Summary

Declares an interface.

Syntax

interface name
    [<param1 [(constraint1[, constraint2 ... [, constraintN]])][, param2 ... [, paramN]]>]
    :
    inherits1[, inherits2 ... [, inheritsN]]
{
    member1;
    member2;
    member3;
}

Parameters

name
The name of the interface. This can be any valid identifier.
paramN
The type parameters of the interface. This is used for creating generic interfaces.
constraintN
The constraints for the type parameters. These can include any reference types.
inheritsN
The interfaces the interface inherits from.
memberN
A member of the interface. Interface members are method signatures.

Description

The interface keyword is used for declaring an interface. Interfaces define the methods that a class must implement by specifying the method signatures (but not the method implementations).

A class method implementing an interface must match the method signature defined in the interface exactly. Furthermore, implementations of a method signature defined in an interface must use the public access modifier. In addition, method implementations must apply either the override or final modifiers. (If methods are removed from the interface, implementing classes will be notified immediately that they are overriding non-existent methods.) A class can implement multiple interfaces.

Interfaces are important because JS++ does not allow multiple inheritance. Since a class can implement multiple interfaces, interfaces can be used to achieve an effect similar to multiple inheritance without the drawbacks (e.g. the "diamond problem").

Interfaces should be named beginning with a capital "I". For more information, see the naming conventions.

Interfaces cannot be directly instantiated. Instead, the class implementing the interface(s) must be instantiated.

Examples

Interface Declaration
1
2
3
4
5
interface ICalculator
{
    int add(int x, int y);
    int minus(int x, int y);
}
Class Implementing Interface
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
interface ICalculator
{
    int add(int x, int y);
    int minus(int x, int y);
}
 
class Calculator : ICalculator
{
    public override int add(int x, int y)
    {
        return x + y;
    }
     
    public override int minus(int x, int y)
    {
        return x - y;
    }
}
Interface with Properties (Getters/Setters)
1
2
3
4
5
6
7
8
interface IRectangle
{
    property int width();
    property int width(int w);
     
    property int length();
    property int length(int l);
}
Class Implementing Multiple Interfaces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
interface IRectangle
{
    property int width();
    property int width(int w);
     
    property int length();
    property int length(int l);
}
interface IArea
{
    int area();
}
 
class Rectangle : IRectangle, IArea
{
    private int _length;
    private int _width;
     
    public Rectangle(int length, int width)
    {
        this._length = length;
        this._width = width;
    }
     
    public override property int width()
    {
        return _width;
    }
    public override property int width(int w)
    {
        this._width = w;
    }
    public override property int length()
    {
        return _length;
    }
    public override property int length(int l)
    {
        this._length = l;
    }
     
    public override int area()
    {
        return this._width * this._length;
    }
}

See Also

External Links

Share

HTML | BBCode | Direct Link