# Kotlin sublass constructors do not inherit default parameter values

**URL:** <https://discuss.kotlinlang.org/t/kotlin-sublass-constructors-do-not-inherit-default-parameter-values/27936>\
**Category:** Language Design\
**Created:** [January 11, 2024, 11:24am UTC](https://discuss.kotlinlang.org/t/kotlin-sublass-constructors-do-not-inherit-default-parameter-values/27936 "2024-01-11T11:24:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![moonlightcheese](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/moonlightcheese/32/10702_2.png) [@moonlightcheese](https://discuss.kotlinlang.org/u/moonlightcheese)\
**Post date:** [January 11, 2024, 11:24am UTC](https://discuss.kotlinlang.org/t/kotlin-sublass-constructors-do-not-inherit-default-parameter-values/27936/1 "2024-01-11T11:24:26Z")

</div>

I’ve come across what appears to be a short-coming of the Kotlin language where it concerns default parameters in constructors. Perhaps there’s something in the documentation that I missed? It appears that default parameters for constructors are not inherited, which is strange, because default parameters for other functions _are_ inherited.

```auto
open class ClassA {
    constructor(
        id: String,
        name: String = "",
    )
}

```

```auto
class ClassB : ClassA {
    constructor(
        id: String,
        name: String, // <-- default value must be explicit, cannot inherit or pass through to super
    ) : super(
        id,
        name, // <-- has default value
    )
}

```

```auto
class ClassC : ClassA {
    constructor( // <-- cannot be instantiated with two constructor arguments
        id: String
    ) : super(
        id // <-- legal call to super constructor, but omits second parameter
    )
}

```

```auto
fun createClass() {
    val a: ClassA =
        ClassA("123")
    val b: ClassB =
        ClassB("123") // <-- illegal syntax, not enough arguments
    val c: ClassC =
        ClassC("123", "Jerry") // <-- illegal syntax, too many arguments
}

```

However, in other functions, default parameters _are_ inherited, like so:

```auto
open class Parent {
    open fun printSomething(argument: String = "default") {} //default value is defined
}

```

```auto
class Child : Parent() {
    override fun printSomething(argument: String){ // no default val specified
        println(argument)
    }
}

```

```auto
fun printTest() {
    Child().printSomething() // compiles successfully, and prints "default"
}

```

Why is it that only constructors have this limitation of unavailability of default parameters through inheritance? This seems like a design flaw. The default value for a constructor should ‘trickle down’ to it’s subclasses the same as it does for other functions. Otherwise refactoring will become exhausting. Say I have a `Parent` class with 16 `Child` classes; I will have to change the default parameters for each constructor in all 17 classes because I’m forced to explicitly declare these values in each subclass. I’ve seen in some examples that we can declare a function which returns the default value, so each constructor (parent and child classes) would call this function to retrieve the default value, but for long parameter lists, this adds unnecessary and undesirable boilerplate code.

**Why are the default parameter values not inherited by subclasses?**

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [January 11, 2024, 1:11pm UTC](https://discuss.kotlinlang.org/t/kotlin-sublass-constructors-do-not-inherit-default-parameter-values/27936/2 "2024-01-11T13:11:04Z")

</div>

Constructor are simply not inherited, and they very likely shouldn’t be, ever. A constructor is inherit to the implementation of this one specific class.  
Note that in your example, the “trickling down” happened because of `override`. If Kotlin had inheriting constructors, then sure this would happen.  
Inheriting constructors is likely never going to happen because then `open` classes can dictate that their constructor is inherited, and hence subclasses can’t declare a more complicated constructor. That’s bad because, again, a subclass might want some required parameters.
