1

I've two question, They may be silly questions but it got me confused now.

QUESTION 1:

I've this code:

private List<Car> carList;

public void setData(List<Object> list)
{
    this.carListist = list;
}

But on this.carList = list I get a compiling error: incompatible types, required: List<Car> but found List<Object>

My question is, why this error? Isn't List a list of objects type Car? So why doesn't it accept an Object there?

QUESTION 2: I change the above code to the following and the error goes away:

private List<Object> carList;

public void setData(List<Object> list)
{
    this.carListist = list;
}


public Object getValueAt(int row, int column) {

    Car car = carList.get(row); //Compiling error here

    switch(column)
    {
        (...)
    }
    (..)

Ther compiling error (signed in the code comment) is incompatible types, required Car but found Object What I don't understand is, now list is defined as an object list. Car is an object. Why car, as being an object, is not accepted?

Anyway to fix this for both question 1 and 2?

dazito
  • 7,740
  • 15
  • 75
  • 117

1 Answers1

1

It is not because every Car is an Object that a list of cars is also a list of objects, in your example what if the list of objects contains a boat and not a car....

GerritCap
  • 1,606
  • 10
  • 9