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 12345interface ICalculator{ int add(int x, int y); int minus(int x, int y);} Class Implementing Interface 123456789101112131415161718interface 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) 12345678interface IRectangle{ property int width(); property int width(int w); property int length(); property int length(int l);} Class Implementing Multiple Interfaces 12345678910111213141516171819202122232425262728293031323334353637383940414243444546interface 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 class Naming Conventions External Links The Diamond Problem Share HTML | BBCode | Direct Link