title: Java field hiding for attributes
body: <p>I just started to learn Java, so please bear with me if the answer is somewhat obvious. I did some research but no avail.</p>
  
  <p>From what I understand, attributes are not overriden but only field hidden. To determine whether the attribute in the superclass or the subclass is used, Java will check the type of the reference.</p>
  
  <p>Then I don't under stand the output here:</p>
  
  <pre><code>public class Super {
      String str = "I'm super!\n";
  
      public String toString() {
          return str;
      }
  }
  
  public class Sub extends Super {
      String str = "I'm sub.\n";
  }
  
  public class TestFH {
      public static void main(String[] args) {
          Sub s1 = new Sub();
  
          System.out.printf(s1.toString());
      }
  }
  </code></pre>
  
  <p>It gives me:</p>
  
  <pre><code>I'm super!
  </code></pre>
  
  <p>I understand that I can achieve what I want easily via method overriding. I'm just curious about what's happenning under the hood.</p>
  
  <p>Thanks in advance.</p>