Java generics and interfaces

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

spencewah
Posts: 17
Joined: Mon Mar 03, 2008 6:00 pm UTC

Java generics and interfaces

Postby spencewah » Fri Apr 25, 2008 7:52 am UTC

So I've got a class, Character, that has this method:

Code: Select all

public ArrayList<Item> getInventory() {
      return inventory;
   }


and it implements an interface, Container, which declares this method:

Code: Select all

public ArrayList<Item> getInventory();


but I'm getting this error in Eclipse: The return type is incompatible with Container.getInventory()

Thoughts? Code looks right to me, I'm not sure what the problem is.

User avatar
jaap
Posts: 2094
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

Re: Java generics and interfaces

Postby jaap » Fri Apr 25, 2008 9:20 am UTC

spencewah wrote:So I've got a class, Character, that has this method:

Code: Select all

public ArrayList<Item> getInventory() {
      return inventory;
   }


and it implements an interface, Container, which declares this method:

Code: Select all

public ArrayList<Item> getInventory();


but I'm getting this error in Eclipse: The return type is incompatible with Container.getInventory()

Thoughts? Code looks right to me, I'm not sure what the problem is.


You haven't shown us the type of the object called inventory which is what you are trying to return.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Java generics and interfaces

Postby Berengal » Fri Apr 25, 2008 11:55 am UTC

If you forgot to parameterize inventory then that might happen.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Java generics and interfaces

Postby EvanED » Fri Apr 25, 2008 10:14 pm UTC

My thoughts: make sure that the classes being referred to are the same, and you don't have two Item classes or something like that.

I *have* actually had errors in C++ where it couldn't convert a type to itself because they were actually in different namespaces.

Waterhouse
Posts: 54
Joined: Wed Oct 03, 2007 12:37 pm UTC

Re: Java generics and interfaces

Postby Waterhouse » Sat Apr 26, 2008 2:49 am UTC

Have you solved it yet? If not, I'd be happy to help you further. It might help if you would show us some more complete code. There isn't enough context to determine what the problem is with the small sample you gave.

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Java generics and interfaces

Postby b.i.o » Sat Apr 26, 2008 11:13 pm UTC

Berengal wrote:If you forgot to parameterize inventory then that might happen.


Yeah, make sure inventory is an ArrayList<Item>...seeing more code would help with this, though.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 9 guests