It means array DOVE[] is considered to be subtype of BIRD[] if DOVE is a subclass of BIRD.
example
Integer[] ints = new Integer[]{9,99};
Number[] nums = ints;
nums[1] = 1.9; // here it will throw array store exception
According to the substitution principle the assignment on the second line is legal
but the exception will be throws at the run time on the third line.
Because whenever an array is created then it tagged with its reified type. here it is integer.
eventhough it is assigned to the Number (super type) type here internally it points to it
Integer type so it throws exception at run time when it detects a float value assigned.
While in contrats for generics the subtype relation is invariant, means
List<DOVE> is not considered to be subtype of List<BIRD>.
example
List<Integer> intList = Arrays.asList(1,2,3,4,5,6,7,8,9);
List<Number> numList = intList; // here it will show compile time error
But here with wild card we can achieve the covariant
in that case here List<DOVE> is considered to be subtype of List<? extends BIRD>.
example
Integer[] ints = new Integer[]{9,99};
Number[] nums = ints;
nums[1] = 1.9; // here it will throw array store exception
According to the substitution principle the assignment on the second line is legal
but the exception will be throws at the run time on the third line.
Because whenever an array is created then it tagged with its reified type. here it is integer.
eventhough it is assigned to the Number (super type) type here internally it points to it
Integer type so it throws exception at run time when it detects a float value assigned.
While in contrats for generics the subtype relation is invariant, means
List<DOVE> is not considered to be subtype of List<BIRD>.
example
List<Integer> intList = Arrays.asList(1,2,3,4,5,6,7,8,9);
List<Number> numList = intList; // here it will show compile time error
But here with wild card we can achieve the covariant
in that case here List<DOVE> is considered to be subtype of List<? extends BIRD>.
No comments:
Post a Comment