Overriding setter property in extending class

#1

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

#2

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

#3

thanks and awesome @tieskedh

0 Likes

#4

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:

2 Likes