title: java inheritance setting value in parent class
body: <p>I have an identical method that is repeated in every subclass and I'd like to refactor it to just one method in a superclass.  </p>
  
  <pre><code>  public class SubClass1 extends SuperClass {
         private BoltHexHead bolt;
         private void computeFoo() {
            //Foo formula is identical in all subclasses. Need to move up
            setFoo(bolt.getDiameter() + bolt.getPitch() + bolt.getTpi());
         }
         private void computeBar() {
            //computeBar method in all subclasses but Bar formula is different amongst all subclasses
            setBar(bolt.getDiameter() - 2*bolt.getPitch() - 3*bolt.getTpi());
         }
         private void computeSeparation() {
            //computeSeparation method only exists for a Subclass 1
            setSeparation(bolt.getLength() - 2*nut.getFlatDia());
         }
  
  
  
    public class SubClass2 extends SuperClass {
         private BoltFlatHead bolt;
         private void computeFoo() {
            //Foo formula is identical in all subclasses. Need to move up
            setFoo(bolt.getDiameter() + bolt.getPitch() + bolt.getTpi());
         }
         private void computeBar() {
            //computeBar method here is different than in Subclass1
            setBar(bolt.getDiameter() - 4*bolt.getPitch() - 1/3*bolt.getTpi());
         }
         private void computeProtrusion() {
            //computeProtrusionmethod only exists for a Subclass 2
            setProtrusionmethod(bolt.getThreadAngle() - 6*bolt.getTpi());
         }
  </code></pre>
  
  <p>Initially I posted that <code>bolt</code> wasn't getting set in the SuperClass but was in the SubClass.  What I got working after my initial post was the following</p>
  
  <pre><code>public abstract class SuperClass {
      protected Bolt&lt;?&gt; bolt;  &lt;-- this was added but uses wildcard
      ...bolt getters/setter
  
         protected void computeFoo() {
            //Foo formula pulled up from all subclasses
            setFoo(bolt.getDiameter() + bolt.getPitch() + bolt.getTpi());
         }
    }
  
  public class SubClass1 extends SuperClass {
    //private BoltHexHead bolt;   &lt;-- commented this out in each subclass
  }
  </code></pre>
  
  <p>This is a JSF app and in each controller bean I instantiate the specific joint attribute subclass and then set the specific bolt. It was an earlier design decision to use setters for setting the bolt (and other properties) in the subclass rather than doing it with the Constructor; but one refactor at a time.</p>
  
  <pre><code>Controller for a Bolt Analysis using a HexHead Bolt
      private SubClass1 sc1 = new SubClass1();
      private BoltHexHead bolt;
      sc1.setBolt(bolt);
      sc1.computeResults();
  
  Controller for a Bolt Analysis using a FlatHead Bolt
      private SubClass2 sc2 = new SubClass2();
      private BoltFlatHead bolt;
      sc2.setBolt(bolt);
      sc1.computeResults();
  </code></pre>
  
  <p>So my question is, is it OK to use wildcard <code>Bolt&lt;?&gt; bolt</code> or is there a better approach?</p>