mardi 23 octobre 2018

Dynamic reference casting depending on actual object type

I have classes hierarchy such as

ChildA extends Parent
ChildB extends Parent
ChildC extends Parent

then in my application I get in a method any on this children by Parent type. The problem is all of these children have the same methods but it for some reasons (I don't know why it was written so) doesn't have their parent.

So, ChildA, ChildB, and ChildC all have getSomeValue() getter but Parent doesn't.

Now I need to parse the value from any of this child, but Parent reference doesn't provide me with API so I need to cast Parent to specific children type.

The question is how to do it properly? I guess it is possible to write using reflection, although I was not able to solve it even with reflection.

Also, even if it possible - is it ok to do that?

FYI: I am working on a legacy application so I can't change the previous written code so I can't add this API in Parent class.





Aucun commentaire:

Enregistrer un commentaire