Hello friends, as discussed in the previous blog we know what are case classes and what boilerplate code do they generate.
Now in this blog, we will be discussing case class inheritance, how it can be achieved as the case to case inheritance is prohibited due to the equals and hashcode method case classes generate. We will also be telling the bad side of case classes.
Case class inheritance breaks equivalence relation (symmetric and transitive). Any case class successor which constricts a definition area must redefine the equality because pattern matching must work exactly as equality.
Now the question arises, how inheritance can be achieved in case classes. The answer is simple: Case Class can extend another Class, trait or Abstract Class.
Using Abstract Class-
- Create an abstract class which encapsulates the common behavior used by all the classes inheriting the abstract class.
- Extend abstract class and create a case class…
View original post 218 more words