title: How do I force any subclasses of my class to always call a parent's implementation method they are overriding?
body: <p>Let's say I have a class, which implements a method (addThings()). It serves as a foundation of a tree of subclasses:</p>
  
  <pre><code>ParentClass {
      protected void addThings() {
          map.add(thing1);
          map.add(thing2);
      }
  }
  </code></pre>
  
  <p>Now, let's say we implement a child class (which has Thing 3 as well) and Thing 3 also needs to be added <strong>on top of Thing 1 and Thing 2</strong>.</p>
  
  <p>The obvious Java solution seems to be to have the child's class's implementation of the method call the super's method:</p>
  
  <pre><code>ChildClass extends ParentClass {
      protected void addThings() {
          super.addThings();
          map.add(thing3);
      }
  }
  </code></pre>
  
  <p><strong>The problem is that whoever implements the subclass may very well forget to do that, and have a bug</strong>:</p>
  
  <pre><code>ChildClassBad extends ParentClass {
      protected void addThings() {
          // BUG!!! Forgot to call super.addThings(); !!!
          map.add(thing3); 
      }
  }
  </code></pre>
  
  <p>Is there a way in Java to <strong>force</strong> any of the extending child (and grandchild) classes to always call a parent's method if they override it? (similar to how making a method abstract always forces them to implement it).</p>
  
  <ul>
  <li><p>Please note that I need the solution to be propagatable down the inheritance tree.</p>
  
  <p>In other words, if someone implements GrandChildClass which needs to add Thing4, they would suffer from the same bug possibility relative to ChildClass.</p>
  
  <p>This means that the simple fix (applicable when you only have 1 level of inheritance) of having separate "addParentThings()" in ParentClass and then calling both addParentThings() and child-overridable empty addThings() is insufficient (<em>because the grandchild has to override non-empty addThings</em>).</p></li>
  </ul>