views:

76

answers:

3

I have

out.load(output, transactions, columnHeaders, dataFormat);

Where load is defined as:

public boolean load(String outputfile, List<Transaction> transactions, List<String> columnHeaders, String dataFormat);

and

String output = "";
String dataFormat = "";
ArrayList<ARTransaction> transactions = new ArrayList<ARTransaction>();
List<String> columnHeaders = null;

where

ARTransaction implements Transaction

Why is there a problem on the type of transactions?

+10  A: 
public boolean load(String outputfile, List<? extends Transaction> transactions, List<String> columnHeaders, String dataFormat);

Or just declare transactions as a List<Transaction>.

Here's the common example of why you obviously can't do this:

List<String> list = new ArrayList<String>();
List<Object> objList = list; //if this were possible
objList.add(Integer.valueOf(5));
String val = list.get(0);  //ClassCastException here
System.out.println(val);
Mark Peters
Just beat me good work
Graphain
Ah. That fixes the problem. ty to both of you. You mean if I wanted to add `BRTransaction t` to `transactions` in the method `load`.That makes sense :-p
piggles
Does that mean that Java will consider transactions to be of type List<ARTransaction> in `load`?
piggles
No, Java will think it is a List of some unknown subclass of Transaction. The actual type in this case will be unknown to `load`. If it matters, consider adding a type parameter to the load method (`T extends Transaction`).
Mark Peters
That's neat. So the type of transaction actually changes (upwards) dynamically! *right?*
piggles
I really don't know what you mean by that so I can't confirm or deny its correctness.
Mark Peters
No, there is nothing dynamic about it. It happens via type erasure at compile time.
EJP
Cool. Ok. I need to go outside. I'm getting excited by Java types.
piggles
+2  A: 

It is having difficulties casting the contravariant type that is from ArrayList<ARTransaction> to List<Transaction>.

Try List<? extends Transaction> instead

Graphain
+1  A: 

Because it may not satisfy the Liskov substitution principle.

duffymo