Override Method with Generics

I have a GenericOrder base class that can be used to create an order with any type of product, then I have subclasses of this order that are more specific. My problem is in my ComputerOrder class and the method that I override. Here is the base class code.

import java.util.List;
import java.util.ArrayList;

public class GenericOrder<T> {

private long orderNumber;
private List<T> products;
private T theClass;

public GenericOrder() 
{
    products = new ArrayList<T>();
    orderNumber = System.currentTimeMillis();
}

public long getOrderNumber() {
    return orderNumber;
}

public void addProduct(T newProduct) {
    products.add(newProduct);

}

public int getNumberOfProducts() {
    return products.size();
}

public List<T> getProducts()
{
    return products;
}

public void setProducts(List<T> products)
{
    this.products = products;
}

public T get()
{
    return theClass;
}

public void set(T theClass)
{
    this.theClass = theClass;
}
}

And here is my subClass code. GetProducts is the method I'm having problems with.

import java.util.ArrayList;
import java.util.List;

public class ComputerOrder<T> extends GenericOrder<T> {
    private List<ComputerPart> computerParts = new ArrayList<ComputerPart>();
    private String orderType = "Computer Parts";

public ComputerOrder() {
    super();

}

public void addProduct(ComputerPart newProduct) {

    computerParts.add(newProduct);

}

public String getOrderType() {
    return orderType;
}

public int getNumberOfProducts() {
    return computerParts.size();
}


public List<T> getProducts()
{

    return computerParts;
}


}

The error I get cannot convert from List(ComputerPart)toList<T>

+4
source share
4 answers

The error is pretty clear: getProducts()declared to return List<T>, but you are returning List<ComputerPart>. I think we agree that the two are not equivalent.

, , , ComputerOrder ComputerPart s. , :

public class ComputerOrder extends GenericOrder<ComputerPart> {
    @Override
    public List<ComputerPart> getProducts() {
        return computerParts;
    }
}
+11

, , , GenericOrder. GenericOrder , , , - . GenericOrder, , PCParts ComputerOrder. . :

public class GenericOrder<T> {    
    private long orderNumber;
    private String orderType;
    private T theClass;

    public GenericOrder(String orderType) {
        this.orderType = orderType;
        orderNumber = System.currentTimeMillis();
    }
    public String getOrderType() {
        return orderType;
    }
    public long getOrderNumber() {
        return orderNumber;
    }
    public T get() {
        return theClass;
    }
    public void set(T theClass) {
        this.theClass = theClass;
    }
}

import java.util.ArrayList;
import java.util.List;

public class PartOrder<T> extends GenericOrder<T> {
    private List<T> parts = new ArrayList<T>();

    public PartOrder(String orderType) {
        super(orderType);
    }
    public void addProduct(T newProduct) {
        parts.add(newProduct);
    }
    public int getNumberOfProducts() {
        return parts.size();
    }
    public List<T> getProducts() {
        return parts;
    }
}

ComputerPartOrder, :

public class ComputerPartOrder extends PartOrder<ComputerPart> {

    public ComputerPartOrder() {
        super("Computer Parts");
    }
}

GenericOrder.getProducts .

+4

, , ComputerOrder . GenericOrder<T> -, ComputerOrder ComputerPart(s) GenericOrder<ComputerPart>. List<ComputerPart> getProducts(), .

0

ComputerOrders , :

public class ComputerOrder extends GenericOrder<ComputerPart> { 

@Override 
public List<ComputerPart> getProducts() { return computerParts; }
 }
0

Source: https://habr.com/ru/post/1620370/


All Articles