DEV Community

shubham mishra
shubham mishra

Posted on • Originally published at Medium

1

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.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more