An interface in Vala is a non-instantiable type. A class may implement any number of interfaces, thereby declaring that an instance of that class should also be considered an instance of those interfaces. Interfaces are part of the GType system, and so compact classes may not implement interfaces (see Classes/Types of class.)
The simplest interface declaration looks like this:
interface InterfaceName {
}
Unlike C# or Java, Vala's interfaces may include implemented methods, and so provide premade functionality to an implementing class, similar to mixins in other languages. All methods defined in a Vala interface are automatically considered to be virtual. Interfaces in Vala may also have prerequisites - classes or other interfaces that implementing classes must inherit from or implement. This is a more general form of the interface inheritence found in other languages. It should be noted that if you want to guarantee that all implementors of an interface are GObject type classes, you should give that class as a prerequisite for the interface.
Interfaces in Vala have a static scope, identified by the name of the interface. This is the only scope associated with them (i.e. there is no class or instance scope created for them at any time.) Non-instance members of the interface (static members and other declarations,) can be identified using this scope.
For an overview of object oriented programming, see Concepts/Object oriented programming.
interface-declaration: [ access-modifier ] interface qualified-interface-name [ inheritance-list ] { [ interface-members ] } qualified-interface-name: [ qualified-namespace-name . ] interface-name interface-name: identifier inheritance-list: : prerequisite-classes-and-interfaces prerequisite-classes-and-interfaces: qualified-class-name [ , prerequisite-classes-and-interfaces ] qualified-interface-name [ , prerequisite-classes-and-interfaces ] interface-members: interface-member [ interface-members ] interface-member: interface-constant-declaration interface-delegate-declaration interface-enum-declaration interface-instance-member interface-static-member interface-inner-class-declaration abstract-method-declaration interface-instance-member: interface-instance-method-declaration interface-instance-abstract-method-declaration interface-instance-property-declaration interface-instance-signal-declaration interface-static-member: interface-static-field-declaration interface-static-method-declaration
As an interface is not instantiable, it may not contain data on a per instance basis. It is though allowable to define static fields in an interface. These are equivalent to static fields in a class: they exist exactly once regardless of how many instances there are of classes that implement the interface.
The syntax for static interface fields is the same as the static class fields: See Classes/Class fields. For more explanation of static vs instance members, see Classes/Types of class members.
Interfaces can contain abstract and non abstract methods. A non-abstract class that implements the interface must provide implementations of all abstract methods in the interface. All methods defined in an interface are automatically virtual.
Vala interfaces may also define static methods. These are equivalent to static methods in classes.
interface-instance-method-declaration: [ class-member-visibility-modifier ] return-type method-name ( [ params-list ] ) method-contracts [ throws exception-list ] { statement-list } interface-instance-abstract-method-declaration: [ class-member-visibility-modifier ] abstract return-type method-name ( [ params-list ] ) method-contracts [ throws exception-list ] ; interface-static-method-declaration: [ class-member-visibility-modifier ] static return-type method-name ( [ params-list ] ) method-contracts [ throws exception-list ] { statement-list }
For discussion of methods in classes, see: Classes/Class methods. For information about methods in general, see Methods. Of particular note is that an abstract method of an interface defines a method that can always be called in an instance of an interface, because that instance is guaranteed to be of a non-abstract class that implements the interface's abstract methods.
Interfaces can contain properties in a similar way to classes. As interfaces can not contain per instance data, interface properties cannot be created automatically. This means that all properties must either be declared abstract (and implemented by implementing classes,) or have explicit get and set clauses as appropriate. Vala does not allow an abstract property to be partially implemented, instead it should just define which actions (get, set or both) should be implemented.
Interfaces are not constructed, and so there is not concept of a construction property in an interface. For more on properties in classes, see: Classes/Properties.
interface-instance-property-declaration: [ class-member-visibility-modifier ] [ class-method-type-modifier ] qualified-type-name property-name { accessors [ default-value ] } ; [ class-member-visibility-modifier ] abstract qualified-type-name property-name { automatic-accessors } ;
Signals can be defined in interfaces. They have exactly the same semantics as when directly defined in the implementing class.
interface-instance-signal-declaration: class-instance-signal-declaration
Constants, Enums, Delegates and Inner Classes all function the same as when they are declared in a class. See Classes. When declared in an interface, all these members can be accessed either using the name of the interface (that is, of the static interface scope), or through and instance of an implementing class.
Demonstrating...
// ...