title: How to access parent class variable in child class inside a child method?
body: <p>I have a class</p>
  
  <pre><code>public class A {
      private String name;
  
      public void setName(String name){
          this.name = name;
      }
  
      public string getName(){return name;}
  
      public String toString() {
         StringBuilder builder = new StringBuilder();
         builder.append(name).append(", ");
         return builder.toString();
      }
  }
  </code></pre>
  
  <p>I have a child class B that extends A and I have to access name from A in toString method in B. I have written the following code but not sure if this a good practice?</p>
  
  <pre><code>public class B extends A {
      private String gender;
  
      public void setGender(String gender){
          this.gender = gender;
      }
  
      public string getGender(){return gender;}
  
      @Override
      public String toString() {
         c = new A();
         StringBuilder builder = new StringBuilder();
         builder.append(c.getName()).append(", ");
         builder.append(gender).append(", ");
         return builder.toString();
      }
  }
  </code></pre>
  
  <p>EDIT:
  If I cannot access private String name in class B, do I have to <code>@Override setName()</code> by using <code>super.setName(name)</code>? In that case how different it is from using class B without extending class A if I dont want to use any of the objects of A?</p>
  
  <p>I just started using JAVA to modify a service.</p>