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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:02:22+00:00 2026-05-14T22:02:22+00:00

Suppose I have the following class heirarchy: class A() class B(a:A) class C(b:B) class

  • 0

Suppose I have the following class heirarchy:

class A()
class B(a:A)
class C(b:B)

class BaseClass(b:B, c:C)

Now I want to implement a subclass of BaseClass, which is given an instance of A, and constructs instances of B and C, which it passes to its superclass constructor.

If I could use arbitrary expressions, I’d do something like this:

b = new B(a)
c = new C(b)
super(b, c)

Because the second argument to the parent constructor depends on the value of the first argument, though, I can’t see any way to do this, without using a factory function, or a gratuitous hack, such as :

class IntermediateSubclass(b:B) extends BaseClass(b, new C(b))
class RealSubclass(a:A) extends IntermediateSubclass(new B(a))

Is there clean way to do this?

  • 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-14T22:02:22+00:00Added an answer on May 14, 2026 at 10:02 pm

    Probably the best way to handle this sort of situation is by writing a factory method in the companion object for the subclass of BaseClass you want to write.

    class A()
    class B(a:A)
    class C(b:B)
    
    class BaseClass(b:B, c:C)
    
    class SBC private (a: A, b: B, c: C)
    extends BaseClass(b, c)
    
    object SBC
    {
        def
        apply(a: A): SBC = {
            val b = new B(a)
            val c = new C(b)
            new SBC(a, b, c)
        }
    }
    

    You can make any of those constructor parameters into fields without affecting anything (by prefixing with val, if you’re not familiar with that syntax):

    class A()
    class B(val a: A)
    class C(val b: B)
    
    class BaseClass(val b: B, val c: C)
    
    class SBC private (val a: A, b: B, c: C)
    extends BaseClass(b, c)
    
    object SBC
    {
        def
        apply(a: A): SBC = {
            val b = new B(a)
            val c = new C(b)
            new SBC(a, b, c)
        }
    }
    

    Now new instances of SBC can be created with this sort of expression: SBC(aValue) (regardless of whether the vals are used).

    scala> val a1 = new A
    a1: A = A@14a8f44
    
    scala> val sbc1 = SBC(a1)
    sbc1: SBC = SBC@7d8bb
    
    scala> sbc1.a
    res0: A = A@14a8f44
    
    scala> sbc1.b
    res1: B = B@c7272
    
    scala> sbc1.c
    res2: C = C@178743b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have the following class hierarchy: class Base { ... }; class Derived1
Suppose I have the following class: public class TestBase { public bool runMethod1 {
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }
Suppose I have the following declaration: class Over1 { protected: class Under1 { };
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require
Suppose I have the following class hierarchy: class A { int foo; virtual ~A()
Suppose I have the following class hierarchy: Class A {...} Class B : A
Suppose you have the following situation #include <iostream> class Animal { public: virtual void
Suppose you have following class: class ProcessController { public List<Process> Active { get {
Suppose I have following string: String asd = this is test ass this is

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.