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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:02:58+00:00 2026-06-03T01:02:58+00:00

I need to execute some code after the subclass already did all it’s initialization,

  • 0

I need to execute some code after the subclass already did all it’s initialization, for example:

abstract class A(a:String) {
  var sum = 0
  def add(n:Int) = { sum += n; sum }
  def verify = if (sum > 10) () else throw new Exception
  ... initialize subclass ...
  verify
}
class B extends A("In A") { 
  val smth = add(50)
  // I want to avoid calling `verify` here
}
val b = new B
println(b.smth) // 50

Is there a way to do it?

  • 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-03T01:03:00+00:00Added an answer on June 3, 2026 at 1:03 am

    So it seems I found the answer. I decided to go with the DelayedInit trait approach – I just execute the delayed code (and count number of times it was executed), then execute the needed code when I think that I’ve seen enough initializations (one for each class in the extension hierarchy). I wrapped it into a trait:

    trait AfterInit extends DelayedInit {
      def afterInit
      private var initCount = 0
      private def getInitNumber(clazz: Class[_]):Int =
        if (clazz.getSuperclass == classOf[java.lang.Object]) 0 else getInitNumber(clazz.getSuperclass) + 1
      final def delayedInit(x: => Unit) {
        x
        initCount += 1
        if (getInitNumber(this.getClass) + 1 == initCount) afterInit
      }
    }
    

    Usage:

    abstract class A(id:String) extends AfterInit {
      var sum = 0
      def add(n:Int) = { sum += n; sum }
      def afterInit = if (sum > 10) () else throw new Exception
    }
    
    class B extends A("B") {
      val add1 = add(50)
    }
    new B // no exception
    
    class C extends A("C") {
      val add2 = add(5)
    }
    new C // exception is thrown, since the `sum` was too small
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to execute some code after each deserialization is done on a POJO's
I want to execute some code right before the layout is rendered, after all
I need to execute some code after the start of the application server (JBoss).
I need execute some code before Windows shutdown process each time. So, I want
In an impersonation scenario related to Sharepoint I need to execute some code in
I need to execute my code after finalization of SysUtils unit. I've placed my
My question is... If we try to execute some code after startActivity() will it
I need to hook into the WCF operation process to execute some code right
I have an HTML textbox that contains some SQL code that I need executed.
I am working on a bash script where I need to conditionally execute some

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.