Question

I have made an interface that covers all classes using generics, however I need additional methods for one class that implements this interface.

I like to be able to use the dynamic binding of declaring variables as the interface, and then initialising it using one of the classes. This way my methods can be much easier to implement.

However, it makes it tricky with one. To try and sum up the program simply, I have an interface:

DataType<T>
{
    public T getParameter();
    public void setParameter(T value);
}

and the classes that implement this type are:

StringData implements DataType<String>
IntData implements DataType<Int>
BooleanData implemets DataType<Boolean>
DoubleData implements DataType<Double>

and the one I want to have additional methods;

ArrayData implements DataType<ArrayList<DataType>>

The ArrayData class contains an ArrayList of the other DataTypes. So I want additional methods for adding, removing and getting a DataType to the list.

However, these methods can't be called when using the Dynamic Binding technique above because not all DataType variables, so I was wondering if anyone know's a way around this, or anything I have missed. Thanks.

Was it helpful?

Solution

As you stated, the method you are proposing is not generic enough for the interface, so to have add/remove methods in the top level interface wouldn't make sense.

Consider 2 interfaces, one that has the basic data operations and one that has list operations.

public interface ListDataType extends DataType<List<DataType>> {
  // list operations
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top