title: How to implement the template method design pattern in Kotlin?
body: <p>Consider the problem:</p>
  
  <p>We have a <code>Base</code> class with an abstract method. Now, we would like to enforce that every override of this method will perform some argument checking or some other drudgery. We want this argument checking to be identical across all overrides. One solution would be to wrap this behavior in a non-abstract method which calls an abstract one:</p>
  
  <pre><code>abstract class Base
  {
      fun computeSomething(argument: Int): Int
      {
          require(argument &gt; 0) // Some intricate checking
          return execute(argument)
      }
  
      // Pure functionality, assuming correct arguments
      // Ideally, this would be private. 
      protected abstract fun execute(validArgument: Int): Int
  }
  
  class Derived1: Base()
  {
      override fun execute(validArgument: Int): Int =
          validArgument * validArgument
  }
  
  class Derived2: Base()
  {
      override fun execute(validArgument: Int): Int =
          validArgument * validArgument * validArgument
  }
  
  
  fun main(args: Array&lt;String&gt;)
  {
      val d = Derived1()
      println(d.computeSomething(23))
  }
  </code></pre>
  
  <p>I would like to have <code>Base::execute</code> private, such that no subclass of <code>Base</code> can call it by accident without checking its arguments. Unfortunately, this is not possible:</p>
  
  <blockquote>
    <p>Error:(9, 5) Kotlin: Modifier 'private' is incompatible with
    'abstract'</p>
  </blockquote>
  
  <p>Kotlin's <code>protected</code> is better than Java's <code>protected</code>, since it makes the function accessible only to subclasses but still, the ideal case would be <code>private</code>.</p>
  
  <p>So is there some right way to implement this pattern? Also, just out of curiosity, is the incompatibility of <code>private</code> and <code>abstract</code> a deliberate choice of language design?</p>