October 2018
Intermediate to advanced
370 pages
9h 15m
English
In our previous example, we can see that the car class implements two interfaces—IDriveable and IFlyable. The tesla class object can run on the road and fly in the sky.
We can restrict an object to a specific interface, even if a class has implemented more than one interface. Let's say we want to create an instance of a normal class that should not contain the functionalities of a flying object. To achieve this, we need to define the type of the interface that we want to expose. Create an object called Toyota, declare it with the IDriveable interface, and initialize it with Car along with the required parameters:
fun main(args: Array<String>) { val toyota : IDriveable = Car(4, "Toyota") toyota.startEngine() toyota.moveForward() ...Read now
Unlock full access