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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:11:47+00:00 2026-06-12T21:11:47+00:00

As far as I’ve learned, traits in Scala are similar to interfaces in Java

  • 0

As far as I’ve learned, traits in Scala are similar to interfaces in Java except methods are allowed to have an implementation. Also, in contrast to Scala classes, you can’t pass them arguments for construction.

So far, so good. But why am I allowed to instantiate them? Really, I don’t see a good reason to allow this.

  • 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-12T21:11:49+00:00Added an answer on June 12, 2026 at 9:11 pm

    You don’t really instantiate them. As you drew a parallel with Java, let’s go further into it. You are able in Java to build a Anonymous class out of an abstract class or of an Interface. It is almost the same in Scala:

    scala> trait A
    defined trait A
    
    scala> new A {}
    res0: A = $anon$1@5736ab79
    

    Note that the curly braces are mandatory when you create an object from a trait. For example, yon cannot do:

    scala> new A
    <console>:9: error: trait A is abstract; cannot be instantiated
                  new A
                  ^
    

    While it would works perfectly for a class:

    scala> class B
    defined class B
    
    scala> new B
    res2: B = B@213526b0
    

    Of course if some elements in your trait are not implemented, you need to implement them when you create the object:

    scala> trait C {def foo: Int}
    defined trait C
    
    scala> new C {}
    <console>:9: error: object creation impossible, since method foo in trait C of type => Int is not defined
                  new C {}
                      ^
    
    scala> new C {def foo = 42}
    res4: C = $anon$1@744957c7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far I have been able to compile my Java programs in Linux using
So far I have only been updated a view from within its controller. I
So far I have been focusing on my application's programming and paid little attention
So far I have tried two approaches: -1. Add to web.xml: <session-config> <tracking-mode>COOKIE</tracking-mode> </session-config>
So far I have been suggesting new features to http://delphi.uservoice.com Since it is managed
So far all the sites I have made have been in PHP, I'm thinking
So far i have the following code, but it doesn't seem to be working,
As far as I know, many nosql stores are written in java, for example
As far as I know, Google Closure Template doesn't allow passing Java object into
So far I have not been able to find an answer, but I wanted

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.