DEV Community

shubham mishra
shubham mishra

Posted on • Originally published at Medium

Scala class inherit or extend another case class not possible

Scala case class inherit , #extend , #case class , #case class #inheritance #code #coding_question

It is not possible that we can not inherite another case class .

If you want to inherite then you can create Trait
Here we create Student Trait
case class Student(identifier: String) {}

case class SportTeam (salary: Long) extends student
Here we define a Student
trait Student{ def identifier: String }

Here we create class which is extends Student
case class SportTeam(identifier: String, salary: Long) extends Student

Case classs not able to extend of another case class because of following -

  1. It led to wrong circumstances and unexpected behaviour.
  2. It produce issue in multiple problems in the pattern matcher.
  3. Case class most consistent with inheritance would violate the spec.
  4. Scala doesn’t generate a copy method if one already exists in the class/traits the case class inherits.

Top comments (0)