title: Inheritance and Overloading methods with different argument data types in Java
body: <p>When I was analyzing a simple java code related with overloading and inheritance I expected to recieve an output that overloads matching the argument's data types. But it doesn't work that way.</p>
  
  <p>Code:</p>
  
  <pre><code>class A {
          public int calc (double num){
              System.out.println("calc A");
              return (int)(num+1);}
      }
  class B extends A{
          public int calc (long num){
              System.out.println("calc B");
              return (int)(num+2);}
      }
  class C extends B{
          public int calc (int num){
              System.out.println("calc C");
              return num+3;}
      }
  class D extends C{
          public int calc (float num){
              System.out.println("calc D");
              return (int)(num+4);}
      }
  
  class Program{
          public static void main(String[] args){
              int num1=10;
              long num2 = num1;
  
              Object o1 = num1;
              System.out.println("num1 Type: "+o1.getClass().getName());
  
              Object o2 = num2;
              System.out.println("num2 Type: "+o2.getClass().getName());
  
              A a1=new D();
              A a2=new D();
  
              System.out.println("a1 Type: "+a1.getClass().getName());
              System.out.println("a2 Type: "+a2.getClass().getName());
  
              int result = a1.calc(num1)+a2.calc(num2);
              System.out.println("Number: "+result);
          }
      }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>num1 Type: java.lang.Integer
  num2 Type: java.lang.Long
  a1 Type: D
  a2 Type: D
  calc A
  calc A
  Number: 22
  </code></pre>
  
  <p>I was testing the code here:
  <a href="http://ideone.com/21RrRn" rel="nofollow">ideone</a></p>