title: Which overloaded method will be called in the given scenario?
body: <p>The output of the given code is </p>
  
  <p>ClassB</p>
  
  <p>ClassA</p>
  
  <pre><code>class Main{
  
  public static void main(String args){
  new Main().getVal(null);  //which method will be called????
  
  A obj=new B();
  new Main().getVal(obj);  //which method will be called????
  
  }
  
  public void getVal(A o){
  System.out.println("class A");
  }
  
  public void getVal(B o){
  
  System.out.println("class B");
  }
  
  }
  
  class A{
  
  }
  
  class B extends A{
  
  
  }
  </code></pre>
  
  <p>So its clear that when null is passed then method with the most child as argument is called and in the second case the call defends on the reference type and not on the object created.
  Sp can anybody give me an explanation why this happens or what happens in java internally?</p>