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 8100937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:51:57+00:00 2026-06-05T22:51:57+00:00

I would like to have a class B which can be extended with two

  • 0

I would like to have a class B which can be extended with two field by two traits A_1 and A_2.
It is important, that class B does not have these two fields by itself, since class B should also be able to be used without these to fields.

My first idea:

trait A_1 { val x: Int }
trait A_2 { val y: Int }
class B

But new B with A_1 with A_2 does not work, since x and y are abstract members and need to be defined in class B.

My second idea:

trait A_1 { var x: Int = _}
trait A_2 { var y: Int = _}
class B

Then one could set the fields after creating a object of B:

val b = new B with A_1 with A_2
b.x = 1
b.y = 2

This works, but is a bit ugly, since the values have to be set afterwards. This construction also makes the class mutable.

My third idea:

class A_1 (val x: Int)
class A_2 (val y: Int)

But class B is not allowed to extend multiple classes, so this idea does not work.

Which possibilites are left to realize this situation ?
Is there a better way than idea 2. ?

  • 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-05T22:51:58+00:00Added an answer on June 5, 2026 at 10:51 pm

    You were so close:

    scala> trait A1 {val x: Int}
    defined trait A1
    
    scala> trait A2 {val y: Int}
    defined trait A2
    
    scala> class B
    defined class B
    
    scala> new B with A1 with A2 {val x = 4; val y = 2}
    res0: B with A1 with A2 = $anon$1@2bc20611
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a class and some attributes which you can either
I would like to have a generic Interpolator class which can interpolate between instances
I have a class which is intended for immutable use, hence I would like
I have an abstract class ClassA which derives from ViewGroup . I would like
I have some classes(call it Class A) which I would like to unit test
I have some XML which I would like to serialize into a class. <Group>
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
I would like to create my own Time class which can accept the hour
I have a class which contains a few boost::numeric::ublas::matrix's within it. I would like
Suppose we have a class which can be serialized/deserialized by XmlSerializer. It would be

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.