C
Cruella DeVille
I have one abstract class Form in which ApplicationForm and
KindergartenForm extends. In my Query class I check wether Form f is
instanceof ApplicationForm vs KindergartenForm and cast f thereafter.
But everytime I call a method from eg. ApplicationForm on f (that is
already casted to ApplicationForm) I have to cast again. Is there a way
to permanently cast f to either ApplicationForm or KindergartenForm or
do I have to cast every time I invoke a method on my f object?
Is it so that I can cast from children to parents without stress, but
the other way around is a repetitive task?
Form form;
public void register(Form f){
if(f istanceof KindergartenForm)
this.form = (KindergartenForm)f;
else if(f instanceof ApplicationForm)
this.form = (ApplicationForm)f;
// do stuff based on type of form
}
This does not permanently cast f to correct type, how come?
KindergartenForm extends. In my Query class I check wether Form f is
instanceof ApplicationForm vs KindergartenForm and cast f thereafter.
But everytime I call a method from eg. ApplicationForm on f (that is
already casted to ApplicationForm) I have to cast again. Is there a way
to permanently cast f to either ApplicationForm or KindergartenForm or
do I have to cast every time I invoke a method on my f object?
Is it so that I can cast from children to parents without stress, but
the other way around is a repetitive task?
Form form;
public void register(Form f){
if(f istanceof KindergartenForm)
this.form = (KindergartenForm)f;
else if(f instanceof ApplicationForm)
this.form = (ApplicationForm)f;
// do stuff based on type of form
}
This does not permanently cast f to correct type, how come?