interfaces in Kotlin

Interfaces define a contract to which different classes can follow. To do that they must override each function and property defined in the interface.

We first declare Buildable interface, which says whatever class conforms to me must declare:

1) val timeRequired of type Int
2) function build

Interfaces cannot have state. Thus, the properties you declare in your interface cannot be initialized.
Classes that conform to these interfaces must declare and initialize this themselves.

Besides declaring a Car type like so:

We can also have type of interface Buildable:

Which then we can only access the function build, and property timeRequired, as specified in the interface. If class Car conformed to other interfaces, we cannot access the functions of the other interfaces because our type is only for Buildable.

Say we a Truck class that also conforms to Buildable. Thus, we can simply have a reference of type Buildable, and call build on the objects like so

Now we are free to change the implementation of function build in class Car and Truck, without affecting this outer code, which does the building. In other words…we can freely change HOW the vehicle is built, without affecting the building of it.