comSysStudent 1 Light Poster

Hi Guys,

These generics are melting my brain.I'm trying to have a list superclass with an arraylist of type T. I then want to invoke the superclass constructor in the subclasses using the type I want to substitute for T, example Product. I'm getting type hiding warnings and quite a few "method undefined for the type Object" errors in the removeProduct() method( I thought <T extends ListItem> would take care of this as Product is a subclass of ListItem. I don't want to paste reams of code so here's what I think are the relevant bits -

public abstract class List<T extends ListItem> 
{
protected ArrayList<T> listItems = new ArrayList<T>();
}
public class stockitem extends List
{

	public <Product>stockitem(String s)
	{
		super(s);
	}
	
public String removeProduct(String code) throws Exception
{
	for(int i = 0; i < listItems.size();i++)
	{
		if(listItems.get(i).toString().startsWith(code))
		{
			if(listItems.get(i).getAmount() > 0)
			{
				return "Error, there are currently " + listItems.get(i).getAmount() + " items of product " + listItems.get(i).getName() + " in stock. \n If you would like to remove this stock entry please return remaining stock to supplier or sell remaining stock";
			}
			String success = "" + listItems.get(i).getCode() + " " + listItems.get(i).getName() + " successfully removed from stock list";
			listItems.remove(i);
			updateList();
			return success;
			
			
		}
	}
	return "Error, no product is currently listed with the provided product code";
}

I feel that I'm very close to cracking this generic thing so any help at all would be greatly appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.