title: Inherit methods for adding to ArrayList in Java
body: <p>I hope you can help me.</p>
  
  <p>I'm working on a "Introduction to programming" Java-project for school, and have a little problem with inheritance.</p>
  
  <p>I need to create methods for adding different products to an arraylist, that represents a very simple database. And so, i have an arraylist for cupcakes, one for bread etc.</p>
  
  <p>How can I create a method in my superclass 'Product', that all the subclasses can inherit.</p>
  
  <p>Right now the 'Add product' is implemented in every subclass, and looks something like this.</p>
  
  <pre><code>protected void addCakes() throws IllegalArgumentException {
      System.out.println("Enter quantity to be added: ");
      int n = scanner.nextInt();
  
      if(n &gt; 0) {
          for(int i = 0; i &lt; n; i++) {
              cupcakedatabase.addCupcake(this);
          }
      } else {
          throw new IllegalArgumentException("The amount has to be positive");
      }
  }
  </code></pre>
  
  <p>and the code in the cupcakeDB looks likes this:</p>
  
  <pre><code>private static ArrayList&lt;Cupcake&gt; cupcakes;
  
  public CupcakeDB() {
      cupcakes = new ArrayList&lt;Cupcake&gt;();
  }
  
  public void addCupcake(Cupcake cupcake) {
      cupcakes.add(cupcake);
  }
  </code></pre>
  
  <p><em>EDIT</em> </p>
  
  <p>This in my product class.</p>
  
  <pre><code>import java.util.Scanner;
  
  public abstract class Product {
  
  protected String name;
  protected String flavor;
  protected double price;
  protected int quantity;
  
  Scanner scanner = new Scanner(System.in);
  
  public void createProduct(String name, String flavor, double price) {
      this.name = name;
      this.flavor = flavor;
      this.price = price;
  }
  
  public void changePrice(double price) {
      this.setPrice(price);
  }
  
  public void changeFlavor(String flavor) {
      this.setFlavor(flavor);
  }
  
  public void setFlavor(String flavor) {
      this.flavor = flavor;
  }
  
  public void setPrice(double price) {
      this.price = price;
  }
  
  public void setQuantity(int quantity) {
      this.quantity = quantity;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getFlavor() {
      return flavor;
  }
  
  public double getPrice() {
      return price;
  }
  
  public String getName() {
      return name;
  }
  
  public int getQuantity() {
      return quantity;
  }
  </code></pre>
  
  <p>}</p>