Im trying to create an instantiate a generic class called "MultipleBoundsClass" that has multiple bounds - a class called "OrderedPair" and an interface called "Pair"(which Ordered Pair implements).
Ive tried removing the interface boundary and that let me compile. But I dont know why that worked, and how Id get it to successfully with the interface boundary included.
public interface Pair<K, V>
{
public K getKey();
public V getValue();
}
public class OrderedPair<K, V> implements Pair
{
private K key;
private V value;
public OrderedPair(K key, V value)
{
this.key = key;
this.value = value;
}
public K getKey()
{
return key;
}
public V getValue()
{
return value;
}
}
class OrderedPair {}
interface Pair {}
public class MultipleBounds<T extends OrderedPair & Pair>
{
private T t;
public MultipleBounds(T t)
{
this.t = t;
}
public T getPair()
{
return t;
}
}
public static void main(String[] args)
{
OrderedPair<String, Integer> p1 = new OrderedPair<>("even", 8);
MultipleBounds<OrderedPair> myPair = new MultipleBounds<OrderedPair>(p1);
}
I get the error "type argument OrderedPair is not within bounds of type-variable T". Bounded types restrict the arguments a generic parameter can be, to the class you define and its subclasses, so why is the type OrderedPair not within bounds of itself when the interface is included as a present boundary?