title: Kotlin - Problem extending a generic class and returning a subtype
body: <p>I have the following situation:</p>
  
  <ul>
  <li>An abstract generic class</li>
  </ul>
  
  <pre class="lang-kotlin prettyprint-override"><code>abstract class K&lt;T&gt; {
    abstract fun bind(itemModel: T)
  }
  </code></pre>
  
  <ul>
  <li>A class that extends from <code>K</code></li>
  </ul>
  
  <pre class="lang-kotlin prettyprint-override"><code>class A : K&lt;ModelA&gt; {
      override fun bind(itemModel: ModelA) {
          // ... do anything
      }
  }
  
  class B : K&lt;ModelB&gt; {
      override fun bind(itemModel: ModelB) {
          // ... do anything
      }
  }
  </code></pre>
  
  
  
  <ul>
  <li>Another class, this is a factory that returns a subtype of K based on some parameter:</li>
  </ul>
  
  <pre class="lang-kotlin prettyprint-override"><code>class Factory {
      companion object {
          const val TYPE_A: Int = 0
          const val TYPE_B: Int = 1
  
          fun create(type: Int): K {
              return when (type) {
                  TYPE_A -&gt; A()
                  TYPE_B -&gt; B()
              }
          }
      }
  }
  </code></pre>
  
  <p>At this point, I'm getting an error in return type section of <code>Factory.create</code> method, it says: "One type argument expected for class K&lt; T >". It's clear that I'm trying to return a subtype of K in <code>create</code> method, and it's the only thing that matters, not the specific type of a generic.</p>
  
  <p>This is possible in <strong>Java</strong>. What is the correct way in Kotlin? </p>