27 Interfaces
IntermediateAn interface is used to define an abstract type. An interface contains no data but can define a behavior of a type. If a type declares that it implements an interface it must implement all member functions.
An interface can contain the following members:
- Member function
- Operator overloading function
- Member property
These members are abstract. Implementing types must have the corresponding implementation of the members.
Here any class that implements interface Ia, must declare and define function f.
A class can implement multiple interfaces separated by &, as shown with class C.
interfaces.cj
interface Ia {
func f(): Unit
}
interface Ib {
func g(): Int64
}
class C <: Ia & Ib {
public func f(): Unit {
println("Function F is implemented")
}
public func g(): Int64 {
println("Function G is implemented")
return 5
}
}
main() {
let classInstance: C = C()
classInstance.f()
classInstance.g()
}
// Output:
// Function F is implemented
// Function G is implemented