How do I force calling a subclasses implementation...
# getting-started
s
How do I force calling a subclasses implementation of a function when a superclass has a function with the same name?
Copy code
open class Foo(val a: Int) {
  fun copy(a: Int = this.a) { ... }
}
class Bar(val a: Int, val b: Int): Foo(a) {
  fun copy(a: Int = this.a, b: Int = this.b) { ... }
  fun bar() {
    copy() //IntelliJ redirects to Foo.copy
  }
}
j
It will pick the function with the fewest parameters. You could put a couple more unused parameters in `Foo.copy()`:
Copy code
open class Foo(val a: Int) {
    fun copy(a: Int = this.a, nothing: Nothing? = null, nothing2: Nothing? = null) { ... }
}
Hacky, but works.
From an API standpoint, you probably should avoid using default parameters with these overloads though, with the ambiguity it creates. What should be the behavior of calling
copy(a = something)
? I would make an explicit no argument function that you can explicitly override in the subclass, and call the class-specific private implementation from the public overridden API.