Dear All,

I am reading Generics, below method is confusing. It clearly mentioned in the Method parameter that it should accept only List. But when i give ArrayList as Argument it compiles. It should be only List right? eventhough Arraylist is an implementaion class. below is the code PLease explain.

public class Quiz extends StringCheck {

    public Quiz() {
        // TODO Auto-generated constructor stub
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub

    ArrayList<Number> input =null;

            List<Number> output = null;

            output = process(input);

        }  


    static <E extends Number> List<E> process(List<E> nums){
        return nums;


    }


}

** Here the input argument is arraylist but not List as specified in the method but still it compiles !! but when i change the output to ArrayList opposing the generic type mentioned in the method it throws error **

Recommended Answers

All 6 Replies

ArrayList implements List, so every ArrayList is also a List.

Can you post the exact syntax you used for the error version?

Please have a look of the below Code which throws complile error

I just Changed the "Output" Variable from List to ArrayList.

public class Quiz extends StringCheck {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

            ArrayList<Number> input =null;

            ArrayList<Number> output = null;

            output = process(input);

       }  


    static <E extends Number> List<E> process(List<E> nums){
        return nums;


    }


}

** Error is below **

Type mismatch: cannot convert from List<Number> to ArrayList<Number>

My doubt is, The code throws error if i change the output variable from List to ArrayList, why its not throwing error for Input Variable which is also an ArrayList

Input: you need a List. You give it an ArrayList which is a subclass All ArrayLists are Lists, so it's OK
Output: You need an ArrayList. YOu give it a List, which is a superclass. Not all Lists are ArrayLists (eg some are LinkedLists), so it's not OK

Thanks James, Understood. This generic type is eating my Brain.

That problem is really nothing to do with generics. It's the same as

String s = "hello";
Object o;
o = s; // this is OK
s = o; // this isn't, even if it would be ok with this particular data
s = (String) o; // is OK, unless there was something at runtime that put a non-String into o

Maybe you are having a problem with the way the generics don't treat subclasses like the rest of Java. A type <T> doesn't include T's subclasses, for that you need some variant of <? extends T>. Most people have a problem with that.
To quote the Oracle tutorials:

In general, if Foo is a subtype (subclass or subinterface) of Bar, and G is some generic type declaration, it is not the case that G<Foo> is a subtype of G<Bar>. This is probably the hardest thing you need to learn about generics, because it goes against our deeply held intuitions.

Thanks James
I think after practising for a while I will get a Hang of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.