title: Java - Overloading and Overriding
body: <pre><code>01    class SubClass extends SuperClass {}
  02    class AppSuperClass {
  03        /**
  04         * @param superClass
  05         */
  06        public void print(SuperClass superClass) {
  07            System.out.println("AppSuperClass:superclass is parameter");
  08     
  09        }
  10        /**
  11         * @param subClass
  12         */
  13        public void print(SubClass subClass) {
  14            System.out.println("AppSuperClass:subclass is parameter");
  15   
  16        }
  17    }
  18   
  19    class AppSubClass extends AppSuperClass {
  20      /**
  21       * @param superClass
  22       */
  23      public void print(SuperClass superClass) {
  24          System.out.println("AppSubClass:superclass is parameter");
  25   
  26      }
  27      /**
  28       * @param subClass
  29       */
  30      public void print(SubClass subClass) {
  31          System.out.println("AppSubClass:subclass is parameter");
  32   
  33      }
  34  }
  35  public class OverloadedTest {
  36      public static void main(String[] args) {
  37          AppSuperClass appSuperClass = new AppSuperClass();
  38          AppSuperClass appSubClass = new AppSubClass();
  39          SuperClass superClass = new SuperClass();
  40          SuperClass subClassInstance = new SubClass();
  41          /*
  42           * Making request to print AppSuperClass
  43           *  1. Passing SuperClass instance
  44           *  2. Passing SubClass instance (*make note of the type) &lt;img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1304052800g" alt=":)" class="wp-smiley"&gt;
  45           */
  46   
  47          appSuperClass.print(superClass);
  48          appSuperClass.print(subClassInstance);
  49          /*
  50           * Above is repeated with AppSubClass instance
  51           */
  52          appSubClass.print(superClass);
  53          appSubClass.print(subClassInstance);
  54      }
  55   
  56  }
  </code></pre>
  
  <p>When I run this I get </p>
  
  <pre><code>       AppSuperClass:superclass is parameter
         AppSuperClass:superclass is parameter
         AppSubClass:superclass is parameter
         AppSubClass:superclass is parameter
  </code></pre>
  
  <p>How can I get </p>
  
  <pre><code>           AppSuperClass:superclass is parameter
             AppSuperClass:subclass is parameter
             AppSubClass:superclass is parameter
             AppSubClass:subclass is parameter
  </code></pre>
  
  <p>as o/p without changing the type of any of the objects?</p>