I have a class (Base) and a field of type InfoBase holding some information. A
specialization (Ext) of Base needs to hold additional information (InfoExt).
Therefore Ext assigns an InfoExt to Base.info. However I ran into problems when
Base replaces info, since it would assign info = new InfoBase()
hence the
additional info of InfoExt is lost.
Therefore I created an abstract void assign()
in Base (variant A). In this
case info needs to be casted to InfoExt everytime it is used in Ext.
In variant B I have thus 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(); }
}
Is this a common situation with a generic way how to deal with it? Are there any drawbacks to variant A/B?
How can I provide an info field in Base, that subclasses can use to store extended information?
Thank you for your consideration
When we inherit class into another class then object of base class is initialized first. If a class do not have any constructor then default constructor will be called. But if we have created any parameterized constructor then we have to initialize base class constructor from derived class.
In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.
By default, a base class can be instantiated by calling its class constructor. You do not have to explicitly define a class constructor. If one is not present in the base class' source code, the C# compiler automatically provides a default (parameterless) constructor.
Whenever we create an object of a class, the default constructor of that class is invoked automatically to initialize the members of the class.
Perhaps something like this is what you are looking for?
abstract class Base {
private InfoBase info;
abstract public void assign();
protected void setInfo(InfoBase info) {
this.info = info;
}
public InfoBase getInfo() {
return info;
}
}
class Ext extends Base {
private InfoExt info;
@Override
public InfoExt getInfo() {
return info;
}
@Override
public void assign() {
info = new InfoExt();
setInfo(info);
}
}
This will allow Base to serve InfoBase
objects, while Ext can serve InfoExt
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With