title: Using reflection to customize private field of superclass
body: <p>I am running into a problem trying to modify the behavior of my superclass' private fields from the sub-class (The superclass wasn't designed to be extended and I can't change that).</p>
  
  <p>Basically, what I have is :</p>
  
  <pre><code>public class OrthoCanvas {
      private OrthoView xy, xz, zy;
  
      public class OrthoView { ... }
  }
  </code></pre>
  
  <p>And I want to do something like that :</p>
  
  <pre><code>public class CustomOrthoCanvas extends OrthoCanvas {
      public CustomOrthoCanvas { 
          // Sets superclass xy, xz, zy to instances of CustomOrthoView
          // This seem to work fine (I'm using reflection to change the fields)
      }
  
      public class CustomOrthoView extends OrthoView { ... }
  }
  </code></pre>
  
  <p>As I said, the reflection seems to work (I'm building CustomOrthoView). But, for the moment, I didn't override any method, and my constructor is just super(whatever), and a Sysout to check what I'm doing. Yet, the original behavior of OrthoView just disappeared, and nothing is working.</p>
  
  <p>Did I make a mistake in my code or is this something more related to my specific case ?</p>
  
  <p>Thanks a lot</p>
  
  <p>Edit : I've just thought that it would be easier if I showed you how I used reflection, so there it is :</p>
  
  <pre><code>Field fieldXY = null;
  Field fieldXZ = null;
  Field fieldZY = null;
  try {
      System.out.println(this.getClass().getSuperclass().getName());
      fieldXY = Class.forName(this.getClass().getSuperclass().getName()).getDeclaredField("xy");
      fieldXZ = Class.forName(this.getClass().getSuperclass().getName()).getDeclaredField("xz");
      fieldZY = Class.forName(this.getClass().getSuperclass().getName()).getDeclaredField("zy");
  } catch (NoSuchFieldException e) {
      System.out.println("-- No such field --");
      System.out.println(e.getMessage());
  } catch (SecurityException e) {
      System.out.println("-- Security failure --");
      System.out.println(e.getMessage());
  } catch (ClassNotFoundException e) {
      System.out.println("-- Class not found --");
      System.out.println(e.getMessage());
  }
  
  fieldXY.setAccessible(true);
  fieldXZ.setAccessible(true);
  fieldZY.setAccessible(true);
  
  try {
      fieldXY.set(this, new CustomOrthoView(this, DimensionId.Z));
      fieldXZ.set(this, new CustomOrthoView(this, DimensionId.Y));
      fieldZY.set(this, new CustomOrthoView(this, DimensionId.X));                
  } catch (IllegalArgumentException e) {
      System.out.println("-- Illegal argument --");
      System.out.println(e.getMessage());
  } catch (IllegalAccessException e) {
      System.out.println("-- Illegal access --");
      System.out.println(e.getMessage());
  }
  </code></pre>
  
  <p>Edit2 : This is a simplified behavior of the superclass :</p>
  
  <pre><code>public class Orthoviewer {
      // This class creates a canvas to display an image
  
      public class OrthoCanvas {
          // This class represents how an image is displayed
          // It implements listeners (to navigate through the image for
          // example), and ways to refresh the image
  
          public class OrthoView extends JPanel {
             // This class displays one part of the image (one plane)
             // To represent a 3D point by the intersection of the three corresponding planes
             // It has an attribute which indicates its dimension 
             // (X is for ZY plane, Y for XZ plane etc)
             // It overrides the method paintComponent to draw itself
  
              public class ImageCache implements Runnable {
                 // This handles the image to display on the corresponding plane
              }
          }
      }
  }
  </code></pre>