0

I have the following classes:

  • parent class CandyBox which has arguments private String flavor and private String origin + method getVolume() which return 0
  • 3 children classes of CandyBox: A, B and C - which have specific additional arguments like height, radius, width, etc, to compute the volume based on the box form

No, I need to create another class, called CandyBag - the idea of this class is to contain an ArrayList which will be a gift composed of one object from class A, one object from class B and one object from class C.

class CandyBag extends CandyBox{

ArrayList<CandyBox> candybag = new ArrayList<CandyBox>();

and I was thinking of declaring object from class A for example:

CandyBox Austria_cherry = new A(20, 5.4, 19.2); 

and then add it to the ArrayList: candybag.add(Austria_cherry);

but I do not think it is a good idea for adding many objects and I also have many errors like expected

I do not know how to structure my code to add these objects from multiple classes in that ArrayList, I am stuck here, can you please help?

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
Aled
  • 1
  • You asked a very similar question yesterday: https://stackoverflow.com/questions/76629324/populating-a-list-from-a-class-with-objects-from-other-classes - Did you fix the error pointed out in the comments, like not trying to call the add method in your class body? – OH GOD SPIDERS Jul 07 '23 at 11:31
  • Yes, I did a method which takes an object parameter of type Candybox to check its instance (the class from where it came from); based on that I add it in the arraylist – Aled Jul 07 '23 at 11:59
  • 1
    Why should `CandyBag` extend `CandyBox`? They have nothing in common. It sounds like `CandyBag` is just a collection of `CandyBox`s. – PPartisan Jul 07 '23 at 12:40

0 Answers0