Covariant Explained
Arrays are Covariant because an Integer is Number and array of Integer is also cosidered as Array of Number.
But generics with simple type parameter are not covariant because List<Integer> is not List<Number> because here polymorphism is apply to base type List only. Polymorphism is not applied at type parameter <Integer> and <Number>.
But you can make the generics covariant using wildcards ? super T and ? extends T.
You can declare this covariant type saftey at class level or method level as per your requirements.
Examples
public interface Circle<T> {
public T get();
public void put(T element);
public void put(Circle<T> circle);
}
Circle<Integer> iCircle = new CircleImpl<Integer>(3);
Number num = iCircle.get(); //
here it will get the integer value in the Number type num variable.
It will not give error because polymorphism is straight forward here.
because You can put subtype value in super type value.
Circle<Number> nCircle = new CircleImpl<Number>(3.2);
Integer i = 3;
nCircle.put(i)
Here you can put subtype value in super type value as per inheritance and polymorphism concept.
Circle<Number> nCircle = new CircleImpl<Number>();
Circle<Integer> iCircle = new CircleImpl<Integer>();
nCircle.put(iCircle); // ERROR
here you cannot put iCircle in the Circle<Number> cirlce tppe argument in put method
because Circle<Number> is not covariant with Circle<Integer> because polymorphism is not applied at the <T> type parameter level in generics. For that we need to use wildcard feature.
To make this work properly you can change the put method like give below
public void put(Box<? extends T> box);
But generics with simple type parameter are not covariant because List<Integer> is not List<Number> because here polymorphism is apply to base type List only. Polymorphism is not applied at type parameter <Integer> and <Number>.
But you can make the generics covariant using wildcards ? super T and ? extends T.
You can declare this covariant type saftey at class level or method level as per your requirements.
Examples
public interface Circle<T> {
public T get();
public void put(T element);
public void put(Circle<T> circle);
}
Circle<Integer> iCircle = new CircleImpl<Integer>(3);
Number num = iCircle.get(); //
here it will get the integer value in the Number type num variable.
It will not give error because polymorphism is straight forward here.
because You can put subtype value in super type value.
Circle<Number> nCircle = new CircleImpl<Number>(3.2);
Integer i = 3;
nCircle.put(i)
Here you can put subtype value in super type value as per inheritance and polymorphism concept.
Circle<Number> nCircle = new CircleImpl<Number>();
Circle<Integer> iCircle = new CircleImpl<Integer>();
nCircle.put(iCircle); // ERROR
here you cannot put iCircle in the Circle<Number> cirlce tppe argument in put method
because Circle<Number> is not covariant with Circle<Integer> because polymorphism is not applied at the <T> type parameter level in generics. For that we need to use wildcard feature.
To make this work properly you can change the put method like give below
public void put(Box<? extends T> box);