I have an abstract class Element:
public abstract class Element{
String nom;
int prix;
// constructor
public Element(String n, int p) {
this.nom = n;
this.prix = p;
}
//getters
public String getNom() {
return nom;
}
public int getPrix() {
return prix;
}
}
And I have a subclass Bag which extends from class Element and is characterised by its weight (poids) and color (couleur). But also a Bag contains a list of Objects (For the Object class).
So what I'm trying to do is create a arrayList<superclass> in my subclass Bag, for then being able to write code to represent these objects: an Apple (price of an apple: 7 rubies), a Banana (price = 5 rubies) and a Fish (price = 20 rubies), also objects can be added or removed to the bag.
My question is:
How to assign the inherited variables to the object arraylist objectsInBag of my "Bag" subclass?
I'm using the code from this question if is good what I'm trying, how should the constructor of the subclass be created, for include the arrayList objectsInBag?
The code I've so far:
import java.util.ArrayList;
public class Bag extends Element{
// atributtes propres a Bag
private String couleur;
private Integer poids;
ArrayList<Object[]> objectsInBag = new ArrayList<>();
// constructor
public Bag(String n, Integer p, String couleur, Integer poids ) {
super(n, p);
this.couleur = couleur;
this.poids = poids;
}
// adding object into the bag
public void addObjects(String n, Integer p){
objectsInBag.add(new Object[]{getNom(), getPrix()});
}
// getters and setters
public String getCouleur(){
return this.couleur;
}
public void setCouleur(String couleur){
this.couleur = couleur;
}
public Integer getPoids(){
return this.poids;
}
public void setPoids(Integer poids){
this.poids = poids;
}