fruit is an abstract class; apple and orange are concrete subclass of fruit.
apple has a special attribute of its own and orange has one as well.
there is an inventory class, it keep that keep track of the numbers of apple and oranges in stock.
I'm trying to get inventory class design right. I have create two arraylists to keep track of two different types of fruit because the constructor for apple and orange is different as they each have an unique attribute. I have been advised that I must not use undefined arraylist type (RAWTYPE). Is there a better way of getting this done? While fruit as an abstract class, I do not have any abstract methods in there, do I have a design issue? Or in this scenario, this is fine as a design approach?
Bear Bibeault wrote:If the inventory class is merely responsible for keep track of numbers of items, why does it need anything other than integer counters?
inventory are arraylisy, they hold objects; e.g. when an orange or apple object is created, it is added to inventory's arraylist
Campbell Ritchie wrote:Beware of anything using instanceof; it can usually be done more elegantly by some other means. Have you considered overloading that putFruit method? Or, go through the Java Tutorials, where you find an example of how you use a Map as a counting device. You can set up a Map with Class<?> objects as its "K" and Integer objects as its "V", and you can count against the Class objects.
Thanks Campbell, in this case, I'm not sure how will I need to overload the putfruit method, as it seems to be rather generic, i want to "dump" all the generic methods that i will be using in the subclass onto the superclass.
Campbell Ritchie wrote:Have you been through the tutorials link I gave you?
If you use that approach, there is no need for any overloading.
Hi Campbell, i'm using arraylist to store the objects instantiated, not simply counting. I'm already making use of collections and its related API. I would appreciate if you would clarify on how the use of Map and overloading methods can apply here. I have gone through these tutorials before though I am not well-versed on them
I think you can use a trick like that to avoid overloading, which will work happily with no changes if you create Banana and Lemon classes. That is the nice thing about object-orientated programming. You can create that sort of technique, and add Bananas with no change to the counting technique whatsoever.
I searched in the tutorial and I found the below sample program. Is this the one you pointed at? I think since String is used, we can use this to count distinct String objects. Can you explain bit more on how to count having a HashMap<Fruit,Integer> to count Apple, Orange objects. I was not able to figure out without using either a instanceof or overloading. Thank you..