Overriding setter property in extending class

Is there any way to override a setter property in extending class?

class A {

var selectedColor: Int
        get() = selectedColor
        set(selectedColor) {
            this.selectedColor = selectedColor
            doSomething()
        }
}

Class B : A {
  override var selectedColor: Int
        get() = selectedColor
        set(selectedColor) {
            this.selectedColor = selectedColor
            doSomethingElse()
        }
}

the keyword override is showing error saying
‘selectedColor’ is final and cannot be overridden
Is there something I am missing?

1 Like

In Kotlin alles is final by default, to make it overridable, you need to add open.
for more info see:

https://kotlinlang.org/docs/reference/classes.html#overriding-properties

1 Like

thanks and awesome @tieskedh

This forum has “likes” for a reason. Use them instead of reopening an old topic. That you don’t send notifications to people who are already part of the topic and I don’t get the topic flagged as unread :wink:

3 Likes