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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:12:59+00:00 2026-06-10T11:12:59+00:00

If class is not a final one, it may be extended. There are two

  • 0

If class is not a final one, it may be extended.

There are two possibilities for values: it may be overridden and should be lazy for it, it may not be overridden and should be final.

If val is final – you may assume that all computations over it would work through class hierarchy. If val may be overriden you should declare it lazy for not becoming broken after extending. You may leave val plain and this gives no guaranties it would be extended in right way.

What use cases imply using plain values?


Example of class initialization failure without lazy values

abstract class A {
  lazy val x1 : String = throw new Exception()
  val x2 : String = "mom"
  val x3 : String = x1 + ", " + x2
  println("A: " + x3)
}
class B extends A {
  override lazy val x1: String = "hello"
  println("B: " + x3)
}
class C extends B {
  override val x2: String = "dad"
  println("C: " + x3)
}

testing it:

scala> new B
A: hello, mom
B: hello, mom
res8: B = B@7e2bd615

it works, but further sub-classing broke already existing functionality

scala> new C
A: hello, null
B: hello, null
C: hello, null
res5: C = C@52a53948

setting lazy on x2 fixes the case:

abstract class A {
  lazy val x1 : String = throw new Exception()
  lazy val x2 : String = "mom"
  val x3 : String = x1 + ", " + x2
  println("A: " + x3)
}
class B extends A {
  override lazy val x1: String = "hello"
  println("B: " + x3)
}
class C extends B {
  override lazy val x2: String = "dad"
  println("C: " + x3)
}

right initialization order:

scala> new C
A: hello, dad
B: hello, dad
C: hello, dad
res6: C = C@5e970110
  • 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-10T11:13:00+00:00Added an answer on June 10, 2026 at 11:13 am

    Basically, you are screwing it up.

    The problem is not the override, the problem is that you are not paying attention to the order in which things are initialized. There are ways to make it work, and ways to make it not work, and you picked the latter. This works:

    scala> class C extends { override val x2: String = "dad" } with B {
         |   println("C: " + x3)
         | }
    defined class C
    
    scala> new C
    A: hello, dad
    B: hello, dad
    C: hello, dad
    res0: C = C@356e3aaf
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a class (not a static class), A , that in
I have a function that accepts a class (not an instance) and, depending on
com/jniwrapper/win32/ie/WebBrowser class not found. From where can I download jniWrapper library. I have downloaded
We have encountered a very strange class not found problem in our web app
I'm trying to call Error(My Test) in normal cpp class (Not a COM object,
Example for threading queue book Accelerated C# 2008 (CrudeThreadPool class) not work correctly. If
We started using Zend Java Bridge, but periodically run into errors like class not
The following class is not thread-safe (as proven in Proving the following code not
My serializable class is not getting read in with objectinputstream after adding static methods
Background The Apache Action class is not thread-safe. However, this was only realized after

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.