October 2018
Intermediate to advanced
370 pages
9h 15m
English
To understand polymorphism, let's create a normal instance of a class. Create a variable and assign a new rectangle object to it:
var rectangle : Rectangle = Rectangle(5.0 , 5.0, "Rectangle")rectangle.draw()
The important point here is that the rectangle variable is a type of Rectangle and the created object is also a Rectangle. Both the reference and the object are same. We can reassign the rectangle variable to a new Rectangle object as many times as we want:
rectangle = Rectangle(10.0 , 7.0, "Another Rectangle")rectangle.draw()
However, we cannot assign a Circle class object to the rectangle variable. If we do, Kotlin will throw a type-mismatch error:
rectangle = Circle(2.5, "Circle")Type mismatch: inferred ...
Read now
Unlock full access