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

  • Home
  • SEARCH
  • 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 8980643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:08:20+00:00 2026-06-15T20:08:20+00:00

I notice that there were several discussions about how to choose between abstract classes

  • 0

I notice that there were several discussions about how to choose between abstract classes and traits, but it seems that none of them focused on the following point. One reason that made me use abstract classes is, they can have constructor parameters, while traits cannot. But why not the following

trait X {
  def haha: Int
}
class Y(val haha: Int) extends X

and early definition is even not necessary to get everything work properly (which I worried about). The abstract class version is

abstract class X(haha: Int)
class Y(val haha: Int) extends X(haha)

and I don’t like the abstract class version because, when you extend several times, these constructor parameters appear everywhere (maybe someone tells me how to avoid this?).

I am aware that abstract classes interpolate with Java better, and match the “is-a” concept more. Despite these, is there any reason that I should use abstract classes somewhere? Thanks!

  • 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-15T20:08:21+00:00Added an answer on June 15, 2026 at 8:08 pm

    The class parameter does not have to be a member (field or def).

    abstract class X(haha: Int) {
      val hoho = 2 * haha  // compile-time constant
    }
    

    Similarly, trait initialization order depends on linearization (mix-in order), which is why trait members should be defs and not vals. (And you can always override the def with a val.) With an abstract class, you know who your supers are, and you’re defining extension points for subclasses.

    But note your abstract class has the val in the wrong place:

    abstract class X(val haha: Int)
    class Y(haha: Int) extends X(haha)
    

    That is, you would expect X to decide if the param is a val (and it doesn’t have to be). Usage of the param in either X or Y could turn it into a field.

    Your observation about value params for classes also applies to type params: What a nuisance to pass Foo[A] up the hierarchy. So in Scala, we can have a member type A instead that can remain abstract until defined in a leaf. But this doesn’t actually bear on whether to define a trait or a class.

    But trait parameters are coming to Scala. (See the Scala bugs for early definitions which are low-priority for this reason.)

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

Sidebar

Related Questions

I notice that there is getWebApplicationContext in org.springframework.web.servlet.mvc.AbstractController . This means that spring programmers
I notice that there is some gap after my table. See the following snapshot:
I was checking out the Tweet# API, and notice that there are 2 ways
I noticed that there's a whole bunch of different content types, some of them
I notice that NetBeans is warning me about using Thread.sleep() in a while loop
I recently noticed that there are several bigint type fields in one of my
I have a repeater containing several several panels. I have noticed that there is
I have been struggling for several hours about this problem, although it seems ridiculous
In several places in our code, we notice that if running under debugger it'll
I have several databound comboboxes in my application and I notice that when i

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.