title: How to put methods in companion object of parent class so that they can be inherited in children classes?
body: <p>I have two data classes which are very similar to each other.
  I want to write a parent class for both of them so they can inherit the common functionality.</p>
  <p>My problem is that some methods I want to inherit are needed to be inside <code>companion object</code>.</p>
  <p>A) <code>data class Link</code></p>
  <pre><code>import org.json.JSONArray
  import org.json.JSONObject
  
  data class Link(
      val name: String,
      val url: String
  ) {
      var selected: Boolean = false
  
      fun toggle() { selected = selected.not() }
  
      companion object {
          fun fromJson(obj: JSONObject): Link = with(obj) {
              Link(getString(&quot;name&quot;), getString(&quot;url&quot;))
          }
  
          fun fromJson(arr: JSONArray): List&lt;Link&gt; = with(arr) {
              List(length()) {
                  fromJson(getJSONObject(it))
              }
          }
  
          fun toJson(list: List&lt;Link&gt;): JSONArray = JSONArray().apply {
              list.forEach {
                  put(it.toJson())
              }
          }
      }
  
      fun toJson(): JSONObject = JSONObject().apply {
          put(&quot;name&quot;, name)
          put(&quot;url&quot;, url)
      }
  }
  
  </code></pre>
  <p>B) <code>data class DownloadStatus</code></p>
  <pre><code>import org.json.JSONArray
  import org.json.JSONObject
  
  data class DownloadStatus(
      val name: String,
      val url: String,
      val path: String,
      var progress: Int = 0
  ) {
      var selected: Boolean = false
  
      fun toggle() { selected = selected.not() }
  
      companion object {
          fun fromJson(obj: JSONObject): DownloadStatus = with(obj) {
              DownloadStatus(getString(&quot;name&quot;), getString(&quot;url&quot;), getString(&quot;path&quot;), getInt(&quot;progress&quot;))
          }
  
          fun fromJson(arr: JSONArray): List&lt;DownloadStatus&gt; = with(arr) {
              List(length()) {
                  fromJson(getJSONObject(it))
              }
          }
  
          fun toJson(list: List&lt;DownloadStatus&gt;): JSONArray = JSONArray().apply {
              list.forEach {
                  put(it.toJson())
              }
          }
      }
  
      fun toJson(): JSONObject = JSONObject().apply {
          put(&quot;name&quot;, name)
          put(&quot;url&quot;, url)
          put(&quot;path&quot;, path)
          put(&quot;progress&quot;, progress)
      }
  }
  </code></pre>
  <p><code>abstract class Parent</code></p>
  <pre><code>abstract class Parent {
      var selected: Boolean = false
  
      fun toggle() { selected = selected.not() }
  
      companion object {
  
      }
  
      abstract fun toJson(): JSONObject
  }
  </code></pre>
  <p>I am stuck here. How to put the methods in <code>companion object</code> of the parent class?</p>