I'm trying to implement this code. Rent is a subclass of Transaction.
import java.util.LinkedList;
public class TransactionList<Transaction> extends LinkedList { //TransactionList: warning: The type parameter Transaction is hiding the type Transaction
public TransactionList<Rent> getRentTransactions(){
TransactionList<Rent> rentList = new TransactionList<Rent>();
for(Transaction t : this){ //this: error: Type mismatch: cannot convert from element type Object to Transaction
if(t instanceof Rent){
t = (Rent) t; //this: warning: Type mismatch: cannot convert from Rent to Transaction
rentList.add((Rent) t);// whole statement: warning: Type safety: The method add(Object) belongs to the raw type LinkedList. References to generic type LinkedList<E> should be parameterized
}
}
return rentList;
}
I'm really lost with this, I'm absolutely sure this code is type safe as any given TransactionList will always contain Transaction or a subclass of Transaction.
However if I change the for statement to
for(Object t : this)
it will compile. However all Objects held by the returned TransactionList will be of the type Object, and unable to be casted to Rent Objects.