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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:30:24+00:00 2026-05-23T15:30:24+00:00

Consider this simple Scala class: class A(val d: Int) Is there a difference in

  • 0

Consider this simple Scala class:

class A(val d: Int)

Is there a difference in Scala (either in behaviour or generated bytecode) between

class B(d: Int) extends A(d)

and

class B(override val d: Int) extends A(d)

or are both equivalent? If they are different, what would be the specific usecase for each of them?

Would it be different if A was defined as class A(var d: Int)?

  • 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-05-23T15:30:25+00:00Added an answer on May 23, 2026 at 3:30 pm

    For vals, there is no semantic difference. However, there may be a difference in the generated bytecode. In particular, if a method defined in the derived class refers to d, it refers to the constructor parameter d rather than to the val of the same name. This is implemented via an additional private field generated for the derived class.

    For vars, there is a difference in behavior. Without an override, any methods that refer to d from within the derived class will be referring to the constructor parameter, while callers referencing d from outside the class will get the field. In this case, the two values may differ (if the value has changed since construction).

    Here’s a session that demonstrates the behavior with a var:

    scala> class A(var d: Int)
    defined class A
    
    scala> class B(d: Int) extends A(d) { override def toString = "d: " + d }
    defined class B
    
    scala> val b = new B(1)
    b: B = d: 1
    
    scala> b.d = 2
    
    scala> b.d
    res1: Int = 2
    
    scala> b
    res2: B = d: 1
    

    This question is related: Idiomatic Scala way to deal with base vs derived class field names?.

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

Sidebar

Related Questions

Consider this simple class: template<class T> class Foo{ public: Foo(T const& val) : _val(val)
Consider a simple table with an auto-increment column like this: CREATE TABLE foo (
Consider this sample code: <div class=containter id=ControlGroupDiv> <input onbeforeupdate=alert('bingo 0'); return false; onclick=alert('click 0');return
Consider this: public class TestClass { private String a; private String b; public TestClass()
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }
I'm designing an HTML page for display in Android browsers. Consider this simple example
Consider the following simple DAG: 1->2->3->4 And a table, #bar, describing this (I'm using
Consider this simple query: SELECT * FROM table1 JOIN table2 USING(pid) WHERE pid='2' ;
Consider the following simple code to create a typesafe equals. This first section allows

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.