0

I have created a class that returns a Hashmap of items:

public class PaymentMap implements Serializable, Map<String, PaymentItem>

but as I have worked with it I have made my life more difficult than it needs to be. The class just needs to be an ArrayList. So I defined the class as :

public class Payments implements Serializable, Collection<PaymentItem>{

This works. Sorry did not do enough searching before posting.

but I get the error:

The type ArrayList can not be a superinterface of Payments, a superinterface must be an interface.

I have done some searching but can't figure out how to define the class statements.

Bill F
  • 2,057
  • 3
  • 18
  • 39
  • http://stackoverflow.com/questions/10435103/the-type-b-cannot-be-a-superinterface-of-c-a-superinterface-must-be-an-interfa – Sotirios Delimanolis Sep 11 '15 at 18:57
  • 1
    You can delete your question, the delete button is next to the edit button. Don't blow up the question though. – Sotirios Delimanolis Sep 11 '15 at 19:00
  • 1
    A class doesn't return anything, it's a representation of some object that has a collection of methods and properties. When a class implements something, that 'something' has to be an interface, and in doing so you are claiming that the class is of that interface's type. – Mage Xy Sep 11 '15 at 19:11
  • @Bill If you're looking to return an _ArrayList_, a (getter or other) method would probably be easiest. If you're looking to have a more unique set of behaviors, you might go for a subclass / private class, which could [implement or extend](http://stackoverflow.com/questions/10839131/implements-vs-extends-when-to-use-whats-the-difference) other interfaces. I generally don't create too many [interfaces](https://docs.oracle.com/javase/tutorial/java/concepts/interface.html) on a day to day basis, but instead focus on classes to create objects with unique properties and behaviors. – Eric McCormick Sep 11 '15 at 19:50

2 Answers2

1

Create your class with interface List

public class Payments implements Serializable, List<PaymentItem> {

    List<PaymentItem> payments = new ArrayList<PaymentItem>();

}

Your Payments class has to have all methods defined by interface List.

Create those automatically:

  • click on word "payments"
  • click on menu "Source / Generate Delegate Methods..."

You can set payments' elements in constructor and in own methods.
Your class Payments will react as an ArrayList<PaymentItem> to the outside and work with the values from payments internally.

Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
  • 1
    Made it a collection and that worked. I have it all working now. I really did not need the HashMap, in this case, it just complicated the whole process. A list might even be better will look into it. – Bill F Sep 11 '15 at 23:23
-1

ArrayList is not an interface and you cannot implement it. ArrayList is a concrete implementation of List interface.

when you say you have created a class that returns HashMap, do you mean method??

yogidilip
  • 790
  • 6
  • 21