Dynamic binding

Runtime polymorphism, which is also known as late or dynamic binding, is used to determine which function to invoke at runtime. When a variable of a superclass is initialized with the object of a child class and the child class provides its own function definition by overriding it, a compiler cannot predict which object type it is and which function should be called. This is also difficult when a list of different shapes is assigned to the shapes list variable:

val shapes : MutableList<Shape> = mutableListOf(triangle,circle)
for (shape in shapes){
shape.draw()
}

When a compiler can't resolve the binding at compile time, this is decided later when the program executes. This technique is called late binding or dynamic binding. All calls to the overridden functions are dynamically bound and resolved at runtime. When a list of randomly inserted objects is passed to the list variable, the compiler can't recognize the shapes in the list so the decision of which function to call is made at runtime.

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

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