I have a generic class which extends another generic class.
The abstract class has 2 type parameters, but I need only one in my functions.
Is it save to just assign it a random like String type, or are there any drawbacks to this?
public abstract class AbstractFoo<T, B>
{
public abstract void read(T item);
}
public class LittleFoo extends AbstractFoo<byte[], String>
{
@Override
public void read(byte[] item)
{
// work here
}
}
No there really isn’t any drawback to this, and it seems like a simple way to do it in my opinion.
Also, you might want to consider using composition instead of extension. For example, take a look at the
HashSetimplementation fromjava.util:A
HashSetis basically aHashMap, but only considers the set of keys (hence the dummy variablePRESENT, which is just a placeholder). You could perhaps do something similar.