DEV Community

Discussion on: Dart for Beginners

Collapse
 
ram535 profile image
aldama • Edited

Wouldn't be this the correct way to write an interface in dart?
When you write an interface like the on below, should we use the '@override' decorator in the implementation? And if we use the @override decorator, is it only for make our intention clear when we are reading the code? because the code works with or without the @override decorator when we write an interface like the one below.

abstract class Shape {
  double area();
  double perimeter();
}
Collapse
 
jvarness profile image
Jake Varness

Traditionally in languages that have OO paradigms, there is some sort of keyword, decorator, or annotation that makes the intent clear that the behavior being defined comes from some other kind of class or interface. @override might not be required, but it makes the intent clearer to others who are looking at your code. That, and if the classes or interfaces it extends from change, this would result in a compiler error since that method isn't truly being overriden anymore.

To your point about interfaces, I think it's perfectly valid to define an interface either way. I would almost say that adding the abstract keyword makes it more like an interface since in my example someone could still construct a Shape whereas in your example they can't.

The only thing I can think of that wouldn't make either of our examples pure interfaces is if the Shape class also included member variables and getters and setters. That would be more along the lines of a traditional abstract class, but is less favorable in OO languages due to the complex nature of extending classes.

Collapse
 
jvarness profile image
Jake Varness

I tried looking in Effective Dart for a more official answer but I couldn't find one ☹️