Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:21:56+00:00 2026-06-04T02:21:56+00:00

I know that you’re not allowed to inherit from case classes but how would

  • 0

I know that you’re not allowed to inherit from case classes but how would you do when you really need to? We have two classes in a hierarchy, both contain many fields, and we need to be able to create instances of both. Here’s my options:

  • If I’d make the super class a usual class instead of a case class – I’d lose all the case class goodness such as toString, equals, hashCode methods etc.
  • If I keep it as a case class, I’d break the rule of not inheriting from case classes.
  • If I use composition in the child class – I’d have to write lots of methods and redirect them to the other class – which would mean lots of work and would feel non-Scalaish.

What should I do? Isn’t it quite a common problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:21:57+00:00Added an answer on June 4, 2026 at 2:21 am

    Yes this is quite a recurrent problem, what I would suggest is to create a trait with all parent properties, create a case class which just implements it and then another one which inherits of it with more properties.

    sealed trait Parent {
      /* implement all common properties */
    }
    
    case class A extends Parent
    
    case class B extends Parent {
      /*add all stuff you want*/
    }
    

    A good way of seeing it is a tree, traits are nodes and case classes are leaves.

    You can use a trait or an abstract class depending on your needs for the parent. However, avoid using a class because you would be able to create instances of it, which would not be elegant.

    EDIT: As suggested in comments, you can seal the trait in order to have exceptions at compilation if not all case classes are covered in a pattern matching. It is for example explained in chapter 15.5 of “Programming in Scala”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this hasn't always been the case, so I have changed something
I know that the entity framework does not currently support geo types but there
We know that IPhone has a push notification function. But I have different kinds
I know that Job.all returns an array of all jobs. But, what would be
I know that ProfileCommon is not available in a Web Application Project (WAP), but
I know that it's a subject that can raise a lot of debate, but
I know that one of the differences between classes and structs is that struct
I know that exceptions have a performance penalty, and that it's generally more efficient
I know that I should put all the html elements in body tag, but
I know that this has already been asked here but the answer (using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.