title: Cast an Object into a class
body: <p>I'm trying to cast an Object into a class instance. The Object of the classes instance should of course inherit all the methods.</p>
  
  <p>So here's my problem in sample code (2) because I can't exactly explain what I mean:</p>
  
  <pre><code>Class Fish {}
  Class Dog {}
  
  class Instances() {
  
      private Fish fish = new Fish();
      private Dog dog = new Dog();
  
      public Object getInstance(String className) {
  
          if( className.equals("fish") ) 
  
              return fish;
  
          else
  
              return dog;
  
      }
  
  }
  
  class test() {
  
      Instances in = new Instances();
  
      public static void main(String[] args) {
  
          // 1: works (transmits all the methods of fish)
          Fish fish = (Fish)in.getInstance("fish");
  
          // 2: doesn't work (fishObj only has access to the methods of an object and can't access the methods of Fish!)
          Object fishObj = null;
          fishObj = (Fish)in.getInstance("fish");
  
      }
  
  }
  </code></pre>
  
  <p>Thank you in advance!</p>