title: In Java how to refer subclass variable without declaring that variable in parent class?
body: <pre><code>public class MyTest {
      public static void main(final String[] args) {
          B b = new B();
          b.print();
      }
  }
  
  class A {
      private final int x = 5;
  
      protected int getX() {
          return x;
      }
  
      public void print() {
          System.out.println(getX());
      }
  }
  
  class B extends A {
      private final int x = 10;
  
      @Override
      protected int getX() {
          return x;
      }
  
  
  }
  </code></pre>
  
  <p>In this example, I need to print subclass value in the parent class.
  It is working fine. No issue.
  Now it is printing 10.
  But I do not want to define that property in the parent <code>class A</code>.</p>
  
  <p>Because in this example this <code>x</code> datatype is very simple. So no issue.
  But in real-time I want to use other datatype which may be another Class variable or <code>List&lt;something&gt;</code> which have huge data.</p>
  
  <p>So ultimately I do not wish to store that value in <code>Class A</code>.
  Because it is redundant data. It will slow down in my Hibernate thing.
  Please let me know, how to achieve this without declaring variable in parent class. But I still need to use subclass variable in parent class.</p>