Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend a base member field?

Tags:

java

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

like image 426
Micha Wiedenmann Avatar asked Jan 08 '13 14:01

Micha Wiedenmann


People also ask

How do you initialize a base class from a derived class?

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.

Is it possible for a class to inherit the constructor of its base 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.

Can you instantiate a base 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.

Which constructor will initialize the base class data member?

Whenever we create an object of a class, the default constructor of that class is invoked automatically to initialize the members of the class.


1 Answers

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.

like image 196
Joel Westberg Avatar answered Sep 28 '22 07:09

Joel Westberg