import java.util.*;
class SampleA { }
class SampleB extends SampleA{ }
class SampleC extends SampleA{ }
class VectorDemo {
public static void main(String r[]) {
Vector<SampleA> v = new Vector<SampleA>();
v.add(new SampleB());
v.add(new SampleC());
SampleC rect = v.get(2);
}
}
The output says "Incompatible types. Found SampleA, required SampleC. SampleC rect = v.get(2);"
My doubt is first of all isn't this like using Wildcard where instead of saying Vector<? extends SampleA> v, we mentioned it a form of Vector<Type> v, which is one of the rules of Wild card which says if only <Type> is mentioned then it can accept only that type and no sub or super type. So why is SampleB and SampleC accepted?
Second, although it's accepted, why v.get(2) is not returning SampleC. Infact writing v.get(1) also gives same error! Why not return SampleB for v.get(1) and SampleC for v.get(2)?
Help please. Thanks.