How do we extend a bounded generic type in Java? For example, I have an abstract class as below:
public abstract class Entry<K, V extends EntryIterable<V>> {
private Entry<K, V> next;
private Entry<K, V> previous;
private K key;
private V value;
}
Let's say I have a couple of methods that I would like to implement in the subclass. So when I try to extend this class I get the below error message.
public class Record<K, V> extends Entry<K, V> {
public Record(K key, V value) {
super(key, value);
}
}
ERROR: Bound mismatch: The type V is not a valid substitute for the bounded parameter <V extends EntryIterable<V>> of the type Entry<K,V>
Please help. Let me know if I made a mistake.
In
Entryclass, you expectVtype to extendsEntryIterable<V>. So inRecordand all its subclasses, as it extendsEntry, you must askVto extendsEntryIterable<V>as well.That's said, this will work:
You could also have stronger type. I mean, if you have a class
EntryIterableAndRemovablethat extendsEntryIterable, you could do: