title: Implicit object type in Java?
body: <p>This isn't exactly the definition of implicit type conversion, but I'm curious how many standards I'm breaking with this one...</p>
  
  <p>I'm creating an abstract class in Java that basically casts its variables depending on a string passed into the constructor.</p>
  
  <p>For example:</p>
  
  <pre><code>public abstract class MyClass {
  
      Object that;
  
      public MyClass(String input){
          if("test1".equals(input){
              that = new Test1();
          }
          else{
              that = new Test();
          }
      }
  
      public void doSomething(){
          if(that instanceof Test1){
              //specific test1 method or variable
          } else if(that instanceof Test2)}
              //specific test2 method or variable
          } else {
              //something horrible happened
          }
      }
  }
  </code></pre>
  
  <p>You see what I'm getting at? Now the problem I run into is that my compiler wants me to explicitly cast <code>that</code> into <code>Test1</code> or <code>Test2</code> in the <code>doSomething</code> method - which I understand, as the compiler won't assume that it's a certain object type even though the if statements pretty much guarantee the type. </p>
  
  <p>I guess what I'm getting at is, is this a valid solution? </p>
  
  <p>I have other classes that all basically do the same thing but use two different libraries depending on a simple difference and figure this class can help me easily track and make changes to all of those other objects.</p>