title: Print Different Object Data From ArrayList in Java
body: <p>Is there some way to print different data from an <code>ArrayList</code> containing different objects?
  For example, I created two basic classes:</p>
  
  <pre><code>import java.util.*;
  
  class Furniture{
      String name;
      int weight;
  
      Furniture(String name, int weight){
          this.name = name;
          this.weight = weight;
      }
  
      String getName(){
          return name;
      }
      int getWeight(){
          return weight;
      }
      }
  }
  class Resident{
      String name;
  
      Resident(String name){
          this.name = name;
      }
  
      String getName(){
          return name;
      }
  }
  </code></pre>
  
  <p>Then I stored them in an <code>ArrayList&lt;Object&gt;</code> and wanted to print the names, by using declared below <code>printArrayList</code> method:</p>
  
  <pre><code>public class Main{
  
  public static &lt;E&gt; void printArrayList(ArrayList&lt;E&gt; arrayToPrint){
      for(E element : arrayToPrint){
          try{
              System.out.println(element.getName());
          }
          catch(Exception e){
              System.out.println("Exception e: " + e);
          }
      }
  }
  
   public static void main(String []args){
      Furniture sofa = new Furniture("Sofa", 5);
      Resident mike = new Resident("Mike");
  
      ArrayList&lt;Object&gt; arrayList = new ArrayList&lt;&gt;();
  
      arrayList.add(sofa);
      arrayList.add(mike);
      printArrayList(arrayList);
  }
  </code></pre>
  
  <p>Now, I know that not all objects can have a variable name or declared get method, therefore I tried to exclude these cases by try/catch.
  I also tried to exclude it by using fe: </p>
  
  <pre><code>if(elements.getName() == null)
  </code></pre>
  
  <p>Still, same results.</p>