title: JAVA: How to call a method in a child class of an Object?
body: <p>i'm trying to solve this problem but really i don't understand the syntax.
  Consider the following code:</p>
  
  <pre><code>class Parent {
     private String productCode1;      // Format X9999
     private String productCode2;
  
      public Parent(String productCode1, String productCode2)
      {
          this.productCode1= productCode1;
          this.productCode2= productCode2;
  
      } 
  
  }
  </code></pre>
  
  <p>then i have a child class wich contain the method i want to call</p>
  
  <pre><code>public Child extends Parent{
      private String[] serialNumbers;
  
      public Child(String productCode1, String productCode2, String... args){
          super(productCode1,  productCode2,);
          int i = 0;
  
          for(String serialN: args){
              serialNumbers = new String[10]; 
              serialNumbers[i]= serialN;
              i++;
          }
  
          public String[] getSerialNumbers(){     //This is the method i want to call
              return this.serialNumbers;
          }
  }
  </code></pre>
  
  <p>this is the inventory class</p>
  
  <pre><code>public class Inventory{
  
  public ArrayList&lt;Parent&gt; products;
  
  public Inventory(){
  
      products = new ArrayList&lt;Parent&gt;();  //Some hard coded items
      products.add(new Parent("A0001", "abcdefg"));
      products.add(new Parent("B0010", "abcdefg"));
      products.add(new Parent("C0100", "abcdefg"));
      products.add(new Child("D1000",  "abcdefg", "firstSN", "secondSN", "thirdSN"));
  }
  
  public static ProductLine getProduct(ArrayList&lt;Parent&gt; products, String productCode){
  
      for(int i = 0; i &lt; products.size(); i++){
           ProductLine product = products.get(i);
           if(product.getProductCode().equals(productCode)){
               return product;  
           }
      }
  
      return null;
  
  }
  </code></pre>
  
  <p>Now in the main class i need to perform a check if the object is a Parent-Object or a Child-Object for do that i'm trying to get a serial number, if i get one it means is a child otherwise is a parent.</p>
  
  <p>The only problem i'm trying to use  the getserialNumbers() which is in the child class</p>
  
  <pre><code>public Application{
      String inputProductCode = keyboard.nextLine(); //Input product code from user
      Inventory inventory = new Inventory();
  
      ProductLine pFound = inventory.getProduct(inventory.products, inputProductCode);  //check if is Child item
  
      String[] serNumb = pFound.getSerialNumbers(); //Here is where i get Error cannot find symbol/method getSerialNumbers
      if(serNumb[0] == null){
            // code continue... 
  }
  </code></pre>
  
  <p>i tried to cast like:</p>
  
  <pre><code>String[] serNumb = ((ItemisedProductLine)pFound).getSerialNumbers();
  </code></pre>
  
  <p>It compiles but he trows(as expected) me an "Parent cannot be cast to child" at runtime.</p>
  
  <p>I hope i explained well 
  sorry for the long question...</p>