title: Two different object types as argument, returning objects of ones type
body: <p>I've such problem - have one abstract class, and many classes that inherits from that class. I have function which gets as arguments objects of that non-abstract classes. It has to return object of non-abstract class, but I know which exectly in runtime. Any ideas?</p>
  
  <p>Here sample code, how its looks like:</p>
  
  <pre><code>public abstract class Shape {
      int x, y;
      void foo();
  }
  
  public class Circle extends Shape {
      int r;
      void bar();
  }
  
  public class Square extends Shape {
      int a;
      void bar();
  }
  </code></pre>
  
  <p>In both classes method bar() do the same thing. And now to do such thing:</p>
  
  <pre><code>/* in some other class */
  public static Shape iHateWinter(Shape a, Shape b) {
      Random rnd = new Random();
      Shape result;
  
      /* 
       btw. my second question is, how to do such thing: 
       a.bar(); ?
      */
  
      if(rnd.nextInt(2) == 0) {
         /* result is type of a */
      } else {
         /* result is type of b */
  }
  </code></pre>
  
  <p>Thanks for help.</p>