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 -
- It led to wrong circumstances and unexpected behaviour.
- It produce issue in multiple problems in the pattern matcher.
- Case class most consistent with inheritance would violate the spec.
- Scala doesn’t generate a copy method if one already exists in the class/traits the case class inherits.
Top comments (0)