I have a class (Base) and an InfoBase type field containing some information. specialization (Ext) of the base should contain additional information (InfoExt). Therefore, Ext assigns InfoExt to Base.info. However, I ran into problems when the Base replaces information, since it assigns info = new InfoBase() , therefore, additional InfoExt information is lost.
Therefore, I created abstract void assign() in the database (option A). In this event information should be sent to InfoExt every time it is used in Ext.
In option B, I additionally created abstract InfoBase info() .
variant A variant B +----------------+ +---------------------------+ +----------------------------+ | InfoBase | | Base | | Base' | |----------------| |---------------------------| |----------------------------| | + name: String | | + info: InfoBase | | + abstract InfoBase info() | | | | + abstract void assign() | | + abstract void assign() | | | | | | | +----------------+ +---------------------------+ +----------------------------+ ^ ^ ^ | | | + + + +----------------+ +---------------------------+ +----------------------------+ | InfoExt | | Ext | | Ext' | |----------------| |---------------------------| |----------------------------| | + id: int | | + void assign() { | | + InfoExt info | | | | info = new InfoExt(); | | + InfoBase info() { | | | | } | | return info; | +----------------+ +---------------------------+ | } | | + void assign() { | | info = new InfoExt(); | | } | +----------------------------+
.
class InfoBase { public String name; } abstract class Base { abstract public void assign(); abstract InfoBase info(); } class InfoExt extends InfoBase { public int id; } class Ext extends Base { public InfoExt info; @Override InfoBase info() { return info; } @Override public void assign() { info = new InfoExt(); } }
This is a common situation with a general way, how to deal with it? Are there any flaws in the A / B option?
How can I provide an information field in Base that subclasses can use to store extended information?
Thank you for your attention
Micha wiedenmann
source share