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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:27:37+00:00 2026-06-13T13:27:37+00:00

given two classes: abstract class ClassA(val argA:Int = 1) { def func1 { …

  • 0

given two classes:

 abstract class ClassA(val argA:Int = 1) {
   def func1 {
     ... argA ... //some operations
     func2
   }
   def func2
 }

class ClassB extends ClassA {
   def func2 {
     ... argA ... //some operations
   }
}

val b = new ClassB
b.func1

How can I pass argA=3 while instantiating b?
There will be dozens of child-classes like B, so it would be good if there were no need to rewrite
“(val argA:Int = 1)” in every class definition. Even in this case, things would be somehow obscure.

The most similar question I have found is about getting, not setting the value:
question

  • 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-13T13:27:38+00:00Added an answer on June 13, 2026 at 1:27 pm

    As you can see, there are several possible ways how to achieve what you asked for. However, it is not obvious which one is the best way, since that depends on what you’d like to put your emphasis on (conciseness, reuse, performance) and how your actual code looks. Some things worth having in mind are:

    With solutions along the line of @maackle’s, each instantiation of the form

    val b = new ClassB { override val argA = 3 }
    

    will result in an anonymous class class AnonSubB extends ClassB that is created behind the scenes. If you want to focus on conciseness, then this is probably the way to go, but the anonymous classes might slow down compilation if their number increases.

    @paradigmatic’s solution

    class ClassB(i: Int) extends ClassA(i) { ... }
    val cb = new ClassB(3)
    

    is nearly as concise and does not result in anonymous classes, but has the disadvantage that the default value of argA as specified by ClassA is not reused (which it is in @maackle’s solution). You could repeat the value, i.e., declare ClassB as

    class ClassB(i: Int = 1) extends ClassA(i) { ... }
    

    but this is error-prone, since you have to change the default value in various places, should it ever need to be changed.

    I also like to offer a solution of my own, where the focus lies on reusing default values without creating anonymous classes, but which is less concise:

    /* ClassA has a reusable (referenceable) default value */
    abstract class ClassA(val argA: Int) {
      def this() = this(ClassA.defaultArgA)
    }
    
    object ClassA {
      val defaultArgA = 1
    }
    
    /* ClassB reuses ClassA's default value */
    class ClassB(x: Int = ClassA.defaultArgA) extends ClassA(x)
    
    /* ClassC has a local (non-referenceable) default value */
    class ClassC(x: Int = 10) extends ClassA(x)
    
    println(new ClassB(2).argA) // 2
    println(new ClassB().argA)  // 1
    println(new ClassC().argA)  // 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is easier to explain with an example. Given these two classes: public class
Given two classes: First class performs AES encryption / decryption and returns encrypted /
Given the following two classes: public class ABC { public void Accept(Ordering<User> xyz) {
Given two classes: class A { private $prop1; } class B extends A {
Given two classes, creating an object of each class in one another results in
Hi all NHibernate gurus ! Given these two classes : public class User {
Given the following two classes: class Location < ActiveRecord::Base belongs_to :holiday_schedule validates :name, :presence
Suppose we have the two classes given below: public class baseclass implements interface {}
Given the following code without considering friendship between two classes: class OutSideClass { ...
I have a problem with these two classes: Creature: public abstract class Creature extends

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.