title: Dynamicaly set class instance variable in java
body: <p>Hi I am working on a project which has a variable which is an instance of the class. The problem is that depending on which option the user picks the instance variable can be on of the classes. </p>
  
  <p>How can I assign a variable to an instance of a class when it can be 1 class or another class?</p>
  
  <p>For example,</p>
  
  <pre><code>//I don't know if this variable is going to be of type class 1 or class 2 
  //at this point any suggestions?
  
  Class1 var1;
  
  if(x == true)
  {
    var1 = new Class1
  }
  else
  {
    var1 = new Class2
  }
  </code></pre>
  
  <p>Thanks in advance!</p>