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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:03:43+00:00 2026-05-15T23:03:43+00:00

I found an error in my scala code, which puzzles me. Below is a

  • 0

I found an error in my scala code, which puzzles me. Below is a simplified version of the problem.

In the constructor of an abstract class, I want to check a few asserts about the abstract methods.
Thus when an object of a subclass is made, these asserts are checked, to see if all is implemented as it should.

It goes wrong when the subclass implements an abstract method using a “val” however:

Scala code:

abstract class A {
    def aval : String
    assert(aval != null, "aval == null")
    assert(aval == "B", "aval: "+aval)
}

class B extends A {
    def aval = "B"
}

class C extends A {
    val aval = "B"
}

object VariousScalaTests {
    def main(args : Array[String]) : Unit = {
        val b = new B
        val c = new C
    }
}

Scala Error:

Exception in thread "main" java.lang.AssertionError: assertion failed: aval == null
    at scala.Predef$.assert(Predef.scala:92)
    at A.<init>(VariousScalaTests.scala:4)
    at C.<init>(VariousScalaTests.scala:12)
    at VariousScalaTests$.main(VariousScalaTests.scala:19)
    at VariousScalaTests.main(VariousScalaTests.scala)

So it fails at the last line of code: “val c = new C”.
Class B works perfectly, but class C doesn’t! The only difference is that C implements aval using “val” and B using “def”.

So my question, most of all, why this difference? I don’t understand what’s going on.

And is there a way to make it work as I want in both cases in scala? Or am I just missing a more elegant way to assert what I want in scala?

  • 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-15T23:03:44+00:00Added an answer on May 15, 2026 at 11:03 pm

    This equivalent Java code should explain the problem:

    public abstract class A {
        public String aval();
    }
    
    public class B extends A {
        public String aval() {
            return "B";
        }
    }
    
    public class C extends A {
        private String _aval;
    
        public C() {
            _aval = "B";
        }
    
        public String aval() {
            return _aval;
        }
    }
    

    When you run

    val c = new C
    

    the constructor of A runs before the constructor of C and the _aval field isn’t assigned yet. So the aval() method returns null (the initial value of the _aval field). But in

    val b = new B
    

    there is no such problem.

    Generally, you should try to avoid calling virtual methods from a constructor.

    And is there a way to make it work as I want in both cases in scala?

    See this question for some approaches.

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

Sidebar

Related Questions

The following code produces no suitable constructor found error. I am unable to figure
Getting Expected ',' or '{' but found '#44559' error. My code looks like this:
I am seting up an existing rails project. But found an error below on
ErrorException [ Fatal Error ]: Class 'Model_Categories' not found MODPATH/orm/classes/kohana/orm.php [ 37 ] 32
Looking at scala's Predef object, which is automatically imported, I found the following gem
This code gives compilation error: import scala.util.continuations._ object CTest { def loop: Nothing =
I have some Scala code I cannot get to compile, and I simplified it
When I'm writing this code, I've got a Compile error in Scala var s:
The following code fails when the constructor parameter is declared with val Works: class
I was following Azure Tutorial and found error while trying to run the Azure

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.