title: In Scala, Refer to Subclass in Abstract Super Class
body: <p>Is there a way for the <code>this</code> keyword in a super class to refer to that class's subclass? Specifically, I am trying to do the following (the Json refers to Play's Json library):</p>
  
  <pre><code>abstract class A() {
    def toJson[T](implicit writes: Writes[T]): JsValue = Json.toJson(this)
  }
  
  case class B(myProperty: String) extends A
  object B { implicit val bFormat = Json.format[B] }
  </code></pre>
  
  <p>This gives the error <code>No Json serializer found for type A. Try to implement an implicit Writes or Format for this type.</code>. So it's saying it can't serialize an object of type <code>A</code>, which makes sense. The goal, however, is for the <code>this</code> in <code>Json.toJson(this)</code> to refer to the subclass (which, in this instance, is <code>B</code>).</p>
  
  <p>Is there any way to accomplish this? If not, is there any other way I can implement the <code>Json.toJson(...)</code> method in the superclass without having to implement in the subclass (and all other subclasses of <code>A</code>)?</p>