title: Object evaluating in java
body: <p>I don't understand, why Java acts the way it does.</p>
  <p>I have two classes,</p>
  <p>Class A:</p>
  <pre><code>public class A {
    public String s = null;
  
    public A(int s) {
      this.s = &quot;a&quot;;
    }
  
    public A(String s) {
      this.s = &quot;b&quot;;
    }
  
    public int f(A p) {
      return 2;
    }
  }
  </code></pre>
  <p>Class B:</p>
  <pre><code>public class B extends A {
    public String s = &quot;c&quot;;
    public B() {
      super(null);
    }
    public B (String s) {
      this (0);
      this . s = s ;
    }
    public B (int x) {
      super (&quot;d&quot;);
    this . s = this . s + x ;
    }
    public int f (A p) {
      return 3;
    }
    public int f(B p) {
      return 4;
  }
  </code></pre>
  <p>If I know have my main class, in which I run the following code:</p>
  <pre class="lang-java prettyprint-override"><code>public class Test {
    Public static main(String[] args) {
      B b1 = new B(&quot;g&quot;);
      A ab = new B(6);
      System.out.println(ab.f(b1));
    }
  }
  </code></pre>
  <p>Why am I getting <code>3</code> as an result, and not <code>2</code> or <code>4</code>, like I would expect?
  Normally I would assume, if I run the method f of the object ab, and give f the object b1 as a parameter, it would either return <code>2</code>, not compile (since the only method f in Class A uses an Object A and not an Object B) or it would look for another method f in Class B, that uses an Object B as a parameter and would execute that, in which case the program should return <code>4</code>.</p>