steinybot
steinybot

Reputation: 6164

What are the risks of explicitly casting from a list of type List<? extends MyObject> to a list of type List<MyObject> in Java?

I think the title should explain it all but just in case...

I want to know what risks and potential issues relating to casting can arise from the following snippet of Java code:

List<? extends MyObject> wildcardList = someAPI.getList();
List<MyObject> typedList = (List<MyObject>) wildcardList;

My thoughts are that all objects in the wildcardList should be an instance of MyObject (exact type or subclass) and so whenever objects are retrieved from typedList then there should never be a ClassCastException. Is this correct? If so why does the compiler generate a warning?

Upvotes: 7

Views: 661

Answers (5)

Zak
Zak

Reputation: 7078

In addition to the answers already posted, take a look at the following

The compiler generates a warning incase an element from the type is accessed later in your code and that is not the generic type defined previously. Also the type information at runtime is not available.

Upvotes: 0

Marimuthu Madasamy
Marimuthu Madasamy

Reputation: 13541

There should be no problem as long as just you retrieve objects from the list. But it could result in runtime exception if you invoke some other methods on it like the following code demonstrate:

    List<Integer> intList = new ArrayList<Integer>();
    intList.add(2);

    List<? extends Number> numList = intList;
    List<Number> strictNumList = (List<Number>) numList;
    strictNumList.add(3.5f);

    int num = intList.get(1); //java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Integer

Upvotes: 7

Andrei Fierbinteanu
Andrei Fierbinteanu

Reputation: 7826

Consider you do something like:

List<ChildClassOne> childClassList = new ArrayList<ChildClassOne>();
childClassList.add(childClassOneInstanceOne);
childClassList.add(childClassOneInstanceTwo);

List<? extends MyObject> wildcardList = childClasslist; // works fine - imagine that you get this from a method that only returns List<ChildClassOne>
List<MyObject> typedList = (List<MyObject>) wildcardList; // warning

typedList.add(childClassTwoInstanceOne); // oops my childClassList now contains a childClassTwo instance
ChildClassOne a = childClassList.get(2); // ClassCastException - can't cast ChildClassTwo to ChildClassOne

This is the only major problem. But if you only read from your list it should be ok.

Upvotes: 1

Rahul
Rahul

Reputation: 21134

This is similar to

List<Object> obj = (List<Object>) new ArrayList<String>();

I hope the problem is evident. List of subtypes can't be cast to Lists of supertypes.

Upvotes: 0

Thomas Kappler
Thomas Kappler

Reputation: 4115

You are correct about retrieving objects from typedList, this should work.

The problem is when you later add more objects to typedList. If, for instance, MyObject has two subclasses A and B, and wildcardList was of type A, then you can't add a B to it. But since typedList is of the parent type MyObject, this error will only be caught at runtime.

Upvotes: 2

Related Questions