Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override setter with call to inherited setter

Tags:

dart

I am a little bit confused: can I override a setter / getter but still use the super setter/getter? If yes - 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 one still preserve the API but expand the logic in the new class. The users of the code already use instance.value = ... so I do not want to change it to method call is possible.

Please help:)

like image 789
Peter StJ Avatar asked Mar 26 '14 12:03

Peter StJ


2 Answers

You can access to parent with super. :

class B extends A {
  void set value(num a) {
    super.value = a;
  }
}
like image 108
Alexandre Ardhuin Avatar answered Oct 22 '22 03:10

Alexandre Ardhuin


Only need call super.value = a

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";
}
like image 20
MiguelAngel_LV Avatar answered Oct 22 '22 03:10

MiguelAngel_LV