View RSS Feed

Advanced Java

Advanced Java programming.

  1. Wildcards with Generics

    by , 11-29-2011 at 11:16 PM
    Unlike with arrays, it is important to remember with generics that is not possible to upcast an object from its superclass to its subclass. So for example, this will create a compile time error.

    Java Code:
    import java.util.*;
    
    public class NonCovariantGenerics { 
    // Compile Error: incompatible types: 
    List<Shape> slist = new ArrayList<Rectangle>();
    In this listing List of Rectangle is not type-equivalent to a List of Shape, even if an Rectangle ...