Design a subclass of a generic container , which should also be type specific

160 views Asked by At

So there is a class called Product. Which has products like ComputerParts and Fruit. I have made a container called genericOrder which accepts type product. I'v added an object of ComputerPart to it. Now i need to Have a subclass of genericOrder called computerOrder which takes the subclasses of ComputerParts which are Ram, CPU etc.

How do i link the container to genericOrder ? i have attached the code below . Forgive me i am very new to programming

import java.util.*;
public abstract class GenericOrder {

    public static void main(String[] args) {
        List < Product > genericOrder = Arrays.asList(new ComputerPart(12), new ComputerPart(76f),
        new Service(26f), new Cheese(26f), new Fruit(26f), new Peripheral(26f));
    }
}
1

There are 1 answers

2
6ton On BEST ANSWER

Use generics. Something on these lines...

public class GenericOrder<T extends Product> {
     public void placeOrder(List<T> products);
}

Then:

public class ComputerOrder extends GenericOrder<ComputerProduct> {
     public void placeOrder(List<ComputerProduct> products)
}