0

I have declared this:

private ArrayList<? extends Transaction> transactionArray;

I want to add subclasses of Transaction to transactionArray, like so:

public void pushTransaction( Transaction theTrans ) {
    transactionArray.add( theTrans ); 
}

But this is rejected with the message

The method … is not applicable for the arguments…

Do I have to write a separate pushTransaction method for every subclass of Transaction? Or how can I make this method accept Transaction and its subclasses as valid arguments? Update: does it make any difference that I have declared Transaction as an abstract class, and am only dealing with its subclasses?

user3238181
  • 115
  • 1
  • 10

1 Answers1

0

You don't need to use the generics syntax. . Your method should work as ling as you are passing a Transaction object or subclass of Transaction. Make sure your class actually extends Transaction. Also there may be more than one class called Transaction so check the package as well.

  • Good tips, thanks; I'll investigate (but the class definitely extends Transaction, with no compile errors). But if this isn't a place to use the generics syntax, then what is it for? – user3238181 Jul 24 '15 at 03:06
  • hi welcome to SOF and please read how to write a good answer here: http://stackoverflow.com/help/how-to-answer. you must provide a code sample or some snippet to help to understand the answer. – Nomesh DeSilva Jul 24 '15 at 03:16