Extend a generic holder class, have it accept only specific objects?

494 views Asked by At

I am trying to understand and accomplish the task of trying to create a class that extends a generic class that accepts all types of classes. So far I have that working. I am trying to create a class that extends a generic holder class and have this class accept only specific objects.

Example, a class called "ComputerOrder" that will not accept an Apple or Orange object but only a ComputerPart or Peripheral object, such as Motherboard or Printer objects. Been stuck on this for 2 weeks. I can't for the life of me figure this concept out. Any help would be appreciated.

abstract class Product{
    protected float price;
    abstract float price();
    public String toString() {
        return "Price = " + String.valueOf(price) + " ";
    }
}

class Apple extends Product{}
class Orange extends Product{}

class ComputerPart extends Product{
    public ComputerPart(float p){
        price = p;
    }
    public float price() { 
        return price; 
    }
}

class Motherboard extends ComputerPart{
    protected String manufacturer;
    public Motherboard(String mfg, float p) {
        super(p);
        manufacturer = mfg;
}
    public String getManufacturer() { 
        return manufacturer; 
    }
}

class Peripheral extends Product{
    public Peripheral(float p) {
        price = p;
    }
    public float price() { 
        return price; 
    }
}

class Printer extends Peripheral{
    protected String model;
    public Printer(String model, float p) {
        super(p);
        this.model = model;
    }
    public String getModel() { 
        return model; 
    }
}

class Cheese extends Product{
    public Cheese(float p) {
        price = p;
    }
    public float price() { 
        return price; 
    }
}

class Cheddar extends Cheese{
    public Cheddar(float p) {
        super(p);
    }
}

class GenericOrder<T>{
    public ArrayList<T> storage = new ArrayList<T>();
    private static int counter = 1;
    public final int id;
    public T obj;
    public GenericOrder(){
        id = counter;
        counter++;
    }
    public void add(T item){
        storage.add(item);
    }
    public T get(int in){
        return obj;
    }
    public void getId(){
        System.out.println(this.id);
    }
    public String toString(){
        String ret = "";
        Iterator<T> it = storage.iterator();
        while (it.hasNext()) {
            ret += it.next() + "\n";
        }
        return ret;
    }
}

class ComputerOrder extends GenericOrder {
    public void add(ComputerPart in){
        if(in instanceof ComputerPart){
            storage.add(in);
        }
    }
}

public class Tme2{
    public static void main(String[] args){
        ComputerOrder com = new ComputerOrder();
        com.add(new Motherboard("bla", 3.33f))
    }
}
1

There are 1 answers

1
thatguy On

You can do it like this:

class ComputerOrder<T extends ComputerProduct> extends GenericOrder<T> {
    //...
}

Here, ComputerProduct is a class that extends Product and all your computer products like ComputerPart or Peripheral extend ComputerProduct. Similarly, you could create a class FoodProduct derived from Product, from which Apple, Orange and Cheese are derived:

class FoodOrder<T extends FoodProduct> extends GenericOrder<T> {
    //...
}

The declaration <T extends ComputerProduct> is a type restriction, which ensures that all types of T are derived from ComputerPart, otherwise you will get a compiler error.

The ComputerOrder class is still generic, so you could instance an order for all computer products:

ComputerOrder order = new ComputerOrder<ComputerProduct>();
// Add peripherals, printers, motherboards...
// Apples, ... will throw compiler errors...

But you could also restrict it to peripherals only:

ComputerOrder order = new ComputerOrder<Peripheral>();
// Add peripherals, printers,...
// Apples, motherboards (ComputerProduct, but NOT Peripheral) will fail...