title: How do you force a subclass to initialize a superclass property at compile-time?
body: <p>I have an abstract superclass with a property that must be set by the subclass for proper operation of instances...</p>
  
  <pre><code>abstract SuperClass {
    // this list must be initialized by the subclass
    List&lt;? extends SavedSearch&gt; savedSearches;
  }
  </code></pre>
  
  <p>This abstract superclass has methods that operate on the savedSearches but it's up to the subclass to ensure that list is properly initialized.</p>
  
  <p>What's the best way to do this?</p>
  
  <p>It's easy with Java create superclass <em>methods</em> that must be implemented by subclasses, but not properties.  I thought about requiring an abstract initSavedSearch method like this in the superclass...</p>
  
  <pre><code>abstract initSavedSearchList();
  </code></pre>
  
  <p>... but that seemed strange, the subclass implementer is under no obligation to do anything in their method implementation, they could just do nothing.</p>
  
  <p>I know I can code up some runtime checks to make sure the savedSearch list is init'd before running code in the superclass, but I'm hoping for compile-time checks (akin to to abstract methods) not runtime checks.</p>
  
  <p>I saw a C# answer to a similar question which suggests hiding the default (zero-arg) constructor and creating a constructor in the abstract class that requires the list as an arg.  That sounds decent for Java, too.  But my particular class is a JSF 1.x managed bean.  I'm a JSF newbie, but AFAIK there's no easy way to get JSF to call a non-standard constructor for Managed Beans.</p>
  
  <p>Is there a way to do this where I get some compile-time help to ensure that implementing subclasses are properly initializing a superclass property?</p>
  
  <p>Thanks!</p>
  
  <ul>
  <li>Gary</li>
  </ul>