How polymorphism works

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 type is Circle but Rectangle was expected

In polymorphism, however, the reference variable and the object can be of a different type. Polymorphism allows us to assign an object of a child class to a reference of a superclass:

var anyShape : Shape = Rectangle(5.0, 9.0, "Rectangle")
anyShape.draw()

When a reference of the parent class is declared, every object of a class that extends the reference type class can be assigned to that variable. In this example, anyShape is a reference of the Shape class and the Rectangle class inherits the Shape class, so the Rectangle class object can be assigned to anyShape.

Execute the following example and notice the convenience of polymorphism:

var anyShape : Shape = Rectangle(5.0, 5.0, "Rectangle")
anyShape.draw()
anyShape = Circle(2.5,"Circle")
anyShape.draw()

Assign a Rectangle object to a shape reference and call the draw function. The draw function from the Rectangle class will be called. Now, reassign a Shape class reference with a Circle class object. This time, the draw function of the Circle class will be executed automatically. The following example will make this clearer. Declare a list of the Shape type and initialize it with a list of different objects that inherit the Shape class:

val shapes : MutableList<Shape> = mutableListOf(triangle, circle, rectangle, cylinder).

Loop through the list of shapes and call the draw function. Each shape will call its own draw function:

for (shape in shapes){
shape.draw()
}

If we add new shapes, such as a line or an obtuse triangle, we can simply add them to the list and the polymorphism technique will handle the rest:

fun main(args: Array<String>) {

var rectangle = Rectangle(_width = 5.0, _height = 5.0, _sName = "Rectangle")
var triangle = Triangle(_base = 6.0, _height = 5.0, _sName = "Triangle")
var circle = Circle(_radius = 2.5, _sName = "Circle")
var cylinder = Cylinder(_radius = 2.5, _height = 4.0, _sName = "Cylinder")

val shapes : MutableList<Shape> = mutableListOf(triangle, circle)

for (shape in shapes){
shape.draw()
}

shapes.add(rectangle)
shapes.add(cylinder)

for (shape in shapes){
shape.draw()
}
}

Let's discuss polymorphism in more detail.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.123.126