Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mix multiple parent class constructors with val in child class

Suppose I have a class Parent that has four fields A, B, C and D, such that C and D are optionally passed or initialized with default implementations:

open class Parent(val a: A, val b: B, val c: C, val d: D) {
    constructor(a: A, b: B, c: C): this(a, b, c, DImpl()){}
    constructor(a: A, b: B): this(a, b, CImpl(), DImpl()){}
}

I need to extend this class and add another field to the child class:

class Child: Parent {
     val e: E // How do I initialize this?
}

Passing a val to a secondary constructor doesn't work, and neither does using the init{} block.

Passing a val to the primary constructor could work, but then I lose the delegation to the secondary constructors in the Parent class - I need to use all the Parent constructor with all the params or duplicate the secondary constructors, leaking implementation details to the Child class.

This should be simple, am I missing something here?

like image 418
npace Avatar asked Dec 05 '22 18:12

npace


1 Answers

If you do need to use secondary constructors, and can't use default values as suggested by @Ingo Kegel, you can initialize the e field just like you would do this in Java, by assigning the parameter value to the property:

class Child: Parent {
    val e: E

    constructor(a: A, b: B, c: C, d: D, e: E) : super(a, b, c, d) {
        this.e = e
    }
}
like image 87
yole Avatar answered Dec 11 '22 09:12

yole