g6848453
g6848453

Reputation: 63

Send object to method

I write the following snippet but for some reason it does not work :

Main:

    TestClasss testClass = new TestClass;

    ArrayList<TestObject> listObj = new ArrayList<>();
    TestObject tb = new TestObject();
    tb.setName("asd");
    tb.setId("13");
    listObj.add(tb);

    testClass.addValue(listObj );

TestClasss

 public void addValalue(ArrayList<Object> list) {
        Log.e("list size is", String.valueOf(list.size()));
    }

I have an error on the following line testClass.addValue(listObj ) :

Error:(47, 11) error: method addValalue in class TestClasss cannot be applied to given types;
required: ArrayList<Object>
found: ArrayList<TestObject>
reason: actual argument ArrayList<TestObject> cannot be converted to ArrayList<Object> by method invocation conversion

I tried to create a class DataObject :

public class DataObject {
}

and extend this class TestObject:

public class TestObject extends DataObject

and change method addValue like this:

public void addValalue(ArrayList<DataObject> list) {
     Log.e("list size is", String.valueOf(list.size()));
}

but still have the same error

Upvotes: 2

Views: 70

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53525

Either change:

public void addValalue(ArrayList<Object> list)

to:

public void addValalue(ArrayList<? extends Object> list)

BTW "Valalue" is not a word in the dictionary (just making sure you haven't confused it with "Value")

Upvotes: 3

Related Questions