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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:33:44+00:00 2026-05-16T15:33:44+00:00

I am experiencing a weird behavior by Scala handling of superclass constructors. I have

  • 0

I am experiencing a weird behavior by Scala handling of superclass constructors.

I have a really simple class defined in the following way

package server

class Content(identifier:String,content:String){
    def getIdentifier() : String    = {identifier}
    def getContent()    : String    = {content}
}

and a simple subclass

package server

class SubContent(identifier:String, content:String) extends Content(identifier, content+"XXX")){

    override def getContent():String = {
        println(content)
        super.getContent
    }
}

What’s really strange is that in the subclass there are duplicates of the superclass attributes, so if i create a new object

var c = new SubContent("x","x")

the execution of

c.getContent

first prints out “x” (The valued provided to the subclass constructor), but returns “xXXX” (The value provided to the superclass constructor).

Is there any way to avoid this behavior? Basically what I’d like to have is that the subclass does not create its own attributes but rather just passes the parameters to the superclass.

  • 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-16T15:33:45+00:00Added an answer on May 16, 2026 at 3:33 pm

    It’s doing exactly what you told it to do. You augmented the 2nd constructor parameter when passing it on to the superclass constructor and then you used the superclass’ getContent to provide the value returned from the subclass’ getContent.

    The thing you need to be aware of is that constructor parameters (those not tied to properties because they’re part of a case class or because they were declared with the val keyword) are in scope throughout the class body. The class’ constructor is that part of its body that is outside any method. So references to constructor parameters in method bodies forces the constructor parameter to be stored in a field so it can have the necessary extent. Note that your println call in getContent is causing such a hidden constructor parameter field in this case.

    Replying to comment “Is there an alternative way to define it in order to avoid this? Or at least, if I never refer to the parameters of the subclass constructors their fields will be allocated (Wasting memory)?”:

    If the only references to plain constructor parameters (*) is in the constructor proper (i.e., outside any method body, and val and var initializers don’t qualify as method bodies) then no invisible field will be created to hold the constructor parameter.

    However, If there’s more you’re trying to “avoid” than these invisible fields, I don’t understand what you’re asking.

    (*) By “plain constructor parameters” I mean those not part of a case class and not bearing the val keyword.

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

Sidebar

Related Questions

I'm experiencing really weird behavior with the Socket.Connect method in C#. I am attempting
I am experiencing weird behavior with template toolkit, say I have a data structure
I am experiencing something weird and have a workaround already, but I don't think
I'm experiencing some weird behavior with SELECT statements in sqlite. There is one table
I'm experiencing a weird issue in which I need to have a default constructor
This is the day of weird behavior. We have a Win32 project made with
I am experiencing weird behavior in IE8 which appears to be an IE bug
I have been experiencing a weird problem that I can't find a solution to.
I am experiencing a weird behavior on my android application. When I open my
Using PHP 5.3 I'm experiencing weird / non-intuitive behavior when applying empty() to dynamic

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.