Extends vs Implements in Dart / Flutter
Extends:
We use extends in Dart/Flutter to implement the inheritance concept of Object Oriented Programming.
Suppose if class B extends class A, then class B, inherits all properties and methods from class A.
example:
class A {
final int id;
A(this.id);
void tellDetails() {
print('my id is: $id');
}
}
class B extends A {
final String name;
B(id, this.name) : super(id);
}
void main() {
var bObj = B(123, 'John Doe');
bObj.tellDetails();
}
In the above example, class A have id property and tellDetails method and class B extends class A. Class B also have its own property name.
Since class B extends class A, we can use all the properties or methods of class A in class B just like we used tellDetails method on bObj object which is an instance of class A.
We cannot extend more than one class in Dart. If you try to extend more than one class then Dart will throw the error “Each class definition can have at most one extends clause”.
This is one of the core concept of Object Oriented Programming in any programming language and Dart is also same here.
Implements:
In Object Oriented Programming, an Interface is just like planning stage which tells, what needs to be implemented in a class, but do not provide any details about How to Implement.
Many programming languages have Interfaces built into the programming language, but, Dart Do not have Interfaces.
Though Dart do not have interfaces, it supports Interfaces concept, not by using a separate interface, but by using a class itself. That means, we can use classes itself to work like an interface.
example:
class A {
final int id;
A(this.id);
void tellDetails() {
print('my id is: $id');
}
}
class B implements A {
@override
final int id;
final String name;
B(this.id, this.name);
@override
void tellDetails() {
print('Id details from B: $id ');
}
}
Unlike with inheritance where you get properties and methods automatically from the parent class, when you are implementing a class, you must implement all the properties and methods of that class.
One more difference between extends(inheritance) and implements is that, you cannot inherit from more than one class but you can implement more than one class.
example:
class A {
final int id;
A(this.id);
void tellDetails() {
print('my id is: $id');
}
}
class C {
final String city = 'ABC';
}
class B implements A, C {
@override
final int id;
@override
final String city = 'XYZ';
final String name;
B(this.id, this.name);
@override
void tellDetails() {
print('Id details from B: $id ');
}
}
void main() {
var bObj = B(123, 'John Doe');
bObj.tellDetails();
}
In this example class B implements both class A and class C.
Though @override is optional in the examples above, it is always recommended to use it. It lets the developer know which are all the properties and methods that must be implemented and which are the properties and methods that are added additionally in the class B.
Using Abstract Classes as Interfaces
We can make use of abstract classes in Dart which acts almost like an interface.
example:
abstract class A {
void tellDetails();
}
class B implements A {
@override
void tellDetails() {
print('Id details from B');
}
}
In this example, you can find that the implementation of tellDetails was removed from class A and was done in class B.
Read more about Classes in Dart