Override the setter with an inherited setter call

I'm a little confused: can I override setter / getter but still use super setter / getter? If so, how?

Use Case:

class A {
  void set value(num a) {
    // do something smart here
  }
}

class B extends A {
  void set value(num a) {
    // call parent setter and then do something even smarter
  }
}

If this is not possible, how can you save the API, but expand the logic in the new class. Code users already use instance.value = ... so I don’t want to change it to a method call.

Please, help:)

+4
source share
2 answers

Access to the parent object can be obtained using super.:

class B extends A {
  void set value(num a) {
    super.value = a;
  }
}
+7
source

Only calling super.value = a is required

class A {
  void set value(String value) {
    print(value.toUpperCase());
  }
}

class B extends A {

  void set value(String value) {
    super.value = value;
    print(value.toLowerCase());
  }
}

void main() {
  B b = new B();
  b.value = "Hello World";
}
+9
source

Source: https://habr.com/ru/post/1533521/


All Articles