interface Statement
Declares the name of an interface, as well as the properties and methods that comprise the interface.
[modifiers] interface interfacename [implements baseinterfaces] {
[interfacemembers]
}
The syntax for interface declarations in JScript is similar to that for class declarations. An interface is like a class in which every member is abstract; it can only contain property and method declarations without function bodies. An interface may not contain field declarations, initializer declarations, or nested class declarations. An interface can implement one or more interfaces by using the implements keyword.
A class may extend only one base class, but a class may implement many interfaces. Such implementation of multiple interfaces by a class allows for a form of multiple inheritance that is simpler than in other object-oriented languages, for example, in C++.
The following code shows how one implementation can be inherited by multiple interfaces.
interface IFormA {
function displayName();
}
// Interface IFormB shares a member name with IFormA.
interface IFormB {
function displayName();
}
// Class CForm implements both interfaces, but only one implementation of
// the method displayName is given, so it is shared by both interfaces and
// the class itself.
class CForm implements IFormA, IFormB {
function displayName() {
print("This the form name.");
}
}
// Three variables with different data types, all referencing the same class.
var c : CForm = new CForm();
var a : IFormA = c;
var b : IFormB = c;
// These do exactly the same thing.
a.displayName();
b.displayName();
c.displayName();
The output of this program is:
This the form name. This the form name. This the form name.