Consider the following c# example:
public class MyParentClass { public int MyInt { get; set; } } public class MyChildClass : MyParentClass { } public class AnotherClass { public MyChildClass GetChildClassFromParentClass(MyParentClass parent) { return new MyChildClass() { MyInt = parent.MyInt }; } }
I’m wondering why it’s not possible to directly clone the parent class into it’s child without the manual step of copying all its values, since MyParentClass
and MyChildClass
share this ‘relationship’.
Maybe such a feature would not be able to guarantee the consistency of MyChildClass
because of missing injected dependencies, but the this language feature could at least permit this cloning if there is a parameterless constructor defined for the child.
Let me be clear I know this feature is not a good idea and there definitely are scenarios which make this a language feature that isn’t feasible or safe. But I’d like to know what those scenarios are.
Minor note: I believe this is not an opinion-based question, since no mainstream OOP language seems to offer this feature, so there must be objective arguments against it.*