How to call the overridden method using polymorphism?

Trying different ways to bring an instance of the class A3 to A2 and call the method implementation of A2. Always invoked method implementation of A3. What am I doing wrong?

package advanced_classes

fun main(args:Array<String>){
 val a = A3()
a.method()

//1
 (a as? A2)?.method()

//2
 if(a is A2){
a.method()
}

}

open class A1{
 open method fun():Unit{
 println("Method from A1")
}
}

open class A2:A1(){
 override the method fun():Unit{
 println("Method from A2")
}
}

open class A3:A2(){
 override the method fun():Unit{
 println("Method from A3")
}
}
April 19th 20 at 12:33
1 answer
April 19th 20 at 12:35
Solution
Briefly: In the framework of the polymorphism and your problem is solved by creating a new object, A2.
Explanation: If you define a method in the class parent, and then override it in the child, it is always method is called, the actual class of the object, i.e. the class whose constructor was called when the object is created. The called method will depend on the type of reference to that object. Due to this, in fact, achieves polymorphism: You don't need to know what kind of implementation of the method of the object that you gave, and all the real class of the object. You declare a reference to an interface/abstract class/open class as a parameter to your function/constructor of your class and work with available this link techniques to do what your function/class is designed. Thereby giving the client the ability to vary the behavior of your code.
Type conversion gives access to a specific class fields, i.e. would be in A2 another method (not the method implementation of the parent and that is something different), after bringing you might call. But this, again, is not about polymorphism.

Find more questions by tags Kotlin