Java field hiding for attributes

field, java

Solution

When you call, `s1.toString()`, it's finding `toString()` method defined only in `Super` class hence using that method as super class methods are available in the sub class. Your super class method `toString()` is using it's own class variable `str` (with value initialized in super class) as the return value from the method and hence the behavior i.e. output as `I'm super!`.

If you want to get the output as `I'm sub.\n` then you need to reuse the same variable as in the super class and assign the new string value i.e. `I'm sub.\n` to it. Best option is to use constructors as:

  public class Super {
     String str = "I'm super!\n";

     public Super(String stringValue){
         this.str = stringValue;
     }

     public String toString() {
        return str;
     }
  }

  public class Sub extends Super {
     public Sub(){
       super("I'm sub.\n");
     }
  }

  public class TestFH {
    public static void main(String[] args) {
       Sub s1 = new Sub();
       System.out.printf(s1.toString());
    }
  }

Problem

I just started to learn Java, so please bear with me if the answer is somewhat obvious. I did some research but no avail. 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. Then I don't under stand the output here: ``` 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()); } } ``` It gives me: ``` I'm super! ``` I understand that I can achieve what I want easily via method overriding. I'm just curious about what's happenning under the hood. Thanks in advance.

Original source