Interfaces
Interfaces in Type-C serve to define contracts for classes or types. These contracts, specified as sets of method signatures, ensure that the types adhering to the interface fulfill certain roles within the system. Hence, given this nature, interfaces types and structure has to be concrete, which sets some limits on what is acceptable in an interface.
Here is an overview of interface constraints:
- Interfaces cannot have fields
- Interfaces supports method's overloading
- Interfaces supports generics
- Interfaces supports operator overloading
- Interfaces cannot have static methods
- Interfaces cannot have constructors (init methods)
Basic Interface Example
Let's consider the common Iterable interface, frequently employed across various programming languages to denote types that can be iterated over.
Here, the Iterable interface requires a method iterator() that must return an object conforming to the Iterator interface. The Iterator interface then outlines the essential methods for iteration.
Implementing Interfaces
The following example demonstrates the implementation of Iterable and Iterator for an array data structure:
Interface inheritance on the other hand is allowed. But it recommended to be used with scare to avoid bloated classes where a lot of methods will have to be defined.