lundi 29 mai 2017

Call method with implicit downcast

I have a List defined as

List<ParentClass> parentClassList;

Parent class is abstract, so when I add elements to the List I do something like

parentClassList.add(new ChildClassOne(...));
parentClassList.add(new ChildClassTwo(...));

and so on... I actually have 5 child classes right now.

What I'd like to do is to call a method in another Class, overwriting its arguments, so:

public void doSomething(ChildClassOne arg) {...}
public void doSomething(ChildClassTwo arg) {...}

But if the type of the List is the parent Class I can't do

doSomething(parentClassList.get(0));

Basically I need to perform different actions based on the child's type and I need access to all the methods inside a specific child. Different childs have different methods, they have only one methods in common.

Aucun commentaire:

Enregistrer un commentaire