title: Java : Using parent class method to access child class variable
body: <p>I have the following scenario :</p>
  
  <pre><code>public class A {
  
      private int x = 5;
  
      public void print()
      {
          System.out.println(x);
      }
  }
  
  
  public class B extends A {
  
      private int x = 10;
  
      /*public void print()
      {
          System.out.println(x);      
      }*/
  
      public static void main(String[] args) {
          B b = new B();
          b.print();
      }
  
  }
  </code></pre>
  
  <p>On executing the code, the output is : 5.</p>
  
  <p>How to access the child class(B's) variable(x) via the parent class method?</p>
  
  <p>Could this be done <strong>without</strong> overriding the print() method (i.e. uncommenting it in B)? </p>
  
  <p>[This is important because on overriding we will have to rewrite the whole code for the print() method again]</p>
  
  <p><strong>EDITED</strong></p>
  
  <p>More Clarification :-</p>
  
  <ul>
  <li>The motive of the question is to use the value of a child class private variable from its parent class method. This doesn't require changing the value of the parent class private variable in order to achieve the desired result.</li>
  <li>The answers posted here, though, led me to my desired answer, which I have posted below.</li>
  </ul>
  
  <p>(Thanks all for your time and help )</p>