Code:
abstract class Parent{
void changeChild(){
}
}
class Child1: Parent{
}
class Child2: Parent{
}
//-----
Parent parent = new Child1(); //instantiated as Child1
parent.changeChild(); //change type of this class to Child2
so parent should be instance of Child2 class.
I understand that Child1 can be different from Child2(more/less fields, methods),
but I just want to call constructor on this object, which is not allowed.
simple
parent = new Child2();
could be done, but there is like 10 child classes(Growing) and I want to move this into parent class
Is this somehow possible in c#?
Thanks
You can’t change the type of an existing object, but you can create a new object and return it.
Example: