title: Is it possible to declare an object of either one class or another in Java?
body: <p>Newbie here. Part of my assignment is to ask the user which of two categories their incoming phone call belongs in. </p>
  
  <p>class CategoryA and class CategoryB are both inherited from the class PhoneCall which we made in a previous assignment.</p>
  
  <p>I want to do this:</p>
  
  <pre><code>    String choice = null;
      System.out.printf("Please enter a 'A' for Category A%n");
      System.out.printf("Please enter a 'B' for Category B%n");
  
      //[Input validation/exception handling stuff goes here]
  
      switch(choice){
          case "A":
              CategoryA objectName = new CategoryA();
              break;
          case "B":
              CategoryB objectName = new CategoryB();
              break;
      }
  </code></pre>
  
  <p>But of course that doesn't work because</p>
  
  <ol>
  <li>I'm declaring objectName inside the switch statement so I can't use it outside of it.</li>
  <li>Both the objects have the same name (even though only one of them would be declared).</li>
  </ol>
  
  <p>The rest of the program uses objectName. If I have to have two different object names then I have to have a if/else statements or switch statements every single time I would have used objectName, right? I can't even pass the object to a function because its either of one class or the other. Is there some solution I am missing here?</p>