Scala class inherit or extend another case class not possible
Scala case class vs abstract class #scala
Updated: January 2025 | By Scala Insights
A case class in Scala is a special type of class designed for immutable data modeling. It comes with several built-in features, including:
case class Student(identifier: String)
As of the latest update in September 2021, Scala does not allow case classes to directly inherit or extend from other case classes. This restriction is enforced by the Scala language design for several reasons:
Case classes come with predefined features like equals
, hashCode
, and copy
methods. If one case class could inherit from another, it would introduce ambiguity in how these features should behave, leading to unexpected results.
Inheritance between case classes could cause problems in the pattern matcher, a core feature of Scala. Pattern matching relies on the structure of case classes, and inheritance could break this functionality.
Allowing case class inheritance would violate Scala’s language specifications, which are designed to ensure consistency and predictability.
Scala generates a copy
method for case classes. If a case class inherits from another class or trait that already has a copy
method, it would lead to conflicts.
Here’s an example that demonstrates why case class inheritance is not allowed:
case class Student(identifier: String)
// This will cause a compilation error
case class SportTeam(salary: Long) extends Student
In this example, the SportTeam
case class attempts to extend the Student
case class, which is not allowed in Scala.
If you need to share common functionality between case classes, Scala provides two alternatives:
Traits are a flexible way to share behavior between classes. Unlike case classes, traits can be mixed into multiple classes.
trait Student {
def identifier: String
}
case class SportTeam(identifier: String, salary: Long) extends Student
In this example, the SportTeam
case class extends the Student
trait, allowing it to share common behavior without violating Scala’s restrictions.
Abstract classes can also be used to share both behavior and fields. However, unlike traits, a class can only extend one abstract class.
abstract class Person {
def identifier: String
}
case class SportTeam(identifier: String, salary: Long) extends Person
Here are the key reasons why Scala case classes cannot extend another case class:
equals
, hashCode
, and copy
.copy
method, it would cause conflicts.Here’s a practical example of how to use traits to share behavior between case classes:
trait Identifiable {
def identifier: String
}
case class Student(identifier: String, age: Int) extends Identifiable
case class SportTeam(identifier: String, salary: Long) extends Identifiable
In this example, both Student
and SportTeam
share the identifier
field through the Identifiable
trait.
Scala case classes are a powerful feature for modeling immutable data, but they come with the limitation of not being able to inherit or extend another case class. This restriction is in place to prevent ambiguity, maintain consistency, and ensure the reliability of features like pattern matching.
Instead of inheritance, Scala developers can use traits or abstract classes to share common functionality between case classes. By following these best practices, you can write clean, maintainable, and efficient Scala code.
Remember: Language features and limitations may change in newer versions of Scala. Always refer to the latest Scala documentation or language specification for the most up-to-date information.