I have a superclass called DataItem
and it has multiple children and the children have children too. I dynamically set the object type but in the end the type reference is DataItem
. Here is the code I use to determine the class type:
private DataItem getDataItemUponType(DataSection parent,Element el) {
String style = getItemStyle(parent,el);
if(style!=null) {
if(style.equals("DUZ")) {
return new DataItemPlain();
} else if(style.equals("TVY")) {
return new DataItemPaired();
} else if(style.equals("TVA")) {
return new DataItem();
} else if(style.equals("FRM")) {
return new DataItemForm();
} else if(style.equals("IMG")) {
return new DataItemImage();
} else if(style.equals("CLN")) {
return new DataItemCalendar();
} else if(style.equals("BTN")) {
return new DataItemButton();
} else if(style.equals("ALT")) {
return new DataItemSubRibbon();
}
} else {
// At least return a DataItem.
return new DataItem();
}
return new DataItem();
}
Then I use this to set my object:
DataItem dataItem = getDataItemUponType(parent,element);
Then I want to call from a subtype of DataItem
. How do I do that?
Note: I need to use DataItem
so making it abstract wouldn't work for me.