Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declaring a method as optional in abstract class

As far as I've understood in Dart is possible to use abstract classes to declare "interfaces" or "protocols" (if you come from objective-c). Anyway I'm having trouble in finding a way to declare an optional method in the abstract class/interface.

If I declare a method in the abstract class A, and let the concrete class B implement A, I get a warning in the compiler. I'd like to be able to declare a method as optional or at least to provide a default implementation without needing to "re-declare" it in a class that implements my interface.

abstract class A{
   void abstractMethod();
}

class B implements A{
 //not implementing abstract method here gives a warning
}
like image 273
dev_mush Avatar asked Aug 03 '16 18:08

dev_mush


2 Answers

That's not how interfaces work. If your class states to implement an interface, then this is what it has to do.

You can split the interface

abstract class A {
   void abstractMethod();
}

abstract class A1 extends A {
   void optionalMethod();
}


class B implements A {
 //not implementing abstract method here gives a warning
}

only when it states to implement A1 it has to implement optionalMethod.

Alternatively you can extend the abstract class

abstract class A{
   void abstractMethod();
   void optionalMethod(){};
}

class B extends A {
 //not implementing abstract method here gives a warning
}

then only abstractMethod needs to be overridden because A doesn't provide an implementation.

like image 125
Günter Zöchbauer Avatar answered Nov 15 '22 07:11

Günter Zöchbauer


Abstract methods defined in classes cannot be marked as optional. (At least not in the regular Dart language, I don't know of annotations that might support something like this.)

Any class that implements an interface must provide an implementation of all abstract methods, but, those method implementations may trivially throw an error to indicate that the method is not available.

  • Throw UnimplementedError if the implementing class is incomplete and the proper implementation is to be added later

  • Throw UnsupportedError if the implementing class does not intend to implement the method.

Note that UnimplementedError implements UnsupportedError.

Obviously you have to be judicious about what you choose to not implement. If it's in code that is not intended to be shared you can get away only implementing methods that you explicitly know are required. If it's in a library package intended to be shared with others you would need a good reason to not implement a method, and that reason should be well documented.

Example code:

abstract class A {
   void abstractMethod();
}

class B implements A {

   void abstractMethod() { throw new UnimplementedError(...); }

   // or

   void abstractMethod() { throw new UnsupportedError(...); }
}

See:

https://api.dartlang.org/stable/1.18.1/dart-core/UnimplementedError-class.html https://api.dartlang.org/stable/1.18.1/dart-core/UnsupportedError-class.html

like image 42
Argenti Apparatus Avatar answered Nov 15 '22 05:11

Argenti Apparatus