Why doesn't Java recognize my ArrayList in an overloaded constructor situation

collections, constructor, java

Solution

Calls first constructor:

new XMessage(information, new ArrayList<Object>());

Calls second constructor:

new XMessage(information, new ArrayList<String>());

`ArrayList<String>()` is not treated as `List<Object>` while `ArrayList<Object>` is. Consider using the following constructor:

public XMessage(Information info, List<? extends Object> results)

as @LuiggiMendoza suggested below.

Problem

I have two constructors setup like so: ``` public XMessage(Information info, List<Object> results) { this.information = info; this.results = results; } public XMessage(Information info, Object result) { this(info, Collections.singletonList(result)); } ``` I create the XMessage object by passing in an Information object and an ArrayList object. When I inspect the result, it is a singleton list wrapping the ArrayList item. Why doesn't Java use the more appropriate constructor and what are my options to force it?

Original source