Extends ,implements and with in Flutter
I know it is daunting as a beginner to hear these 3 words again and again , what it means , how it works ,no proper docs clearly explains it with example. I too got struck in this dilemma and couldn’t understand what these words actually meant.
So, lets jump right into it , see there are certain reserved words in most programming language [ I think all still to be on the safe side let’s use the word most] which convey specific meaning to compiler and cannot be used as an identifier in other places.
These 3 are just the same they are also keywords , but what do they tell to the compiler? Let’s take each keyword one by one
extends->(Inheritance)used to inherit properties and functionalities from another class.
implements->(Interface)used to implement all the features defined in the class.
with ->(Mixins) way to inherit from multiple classes
Let’s take example to get cleared
class Abc{
int a=10;
void fun(){
print('hello');
}
}
class ExAbc extends Abc{
int b=20;
void nobita(){
print('hii');
}
}
void main(){
ExAbc exabc=ExAbc();
exabc.fun();
exabc.nobita();
}
Now with the help of extends , i am extending a class ie all the functions and properties are available in the derived class and in the derived class , i can add more features and properties. Even if i want i can override functions of superclass
class Abc{
int a=10;
void fun(){
print('hello');
}
}
class ExAbc extends Abc{
int b=20;
void nobita(){
print('hii');
}
@override
void fun(){
print('Bye');
}
}
void main(){
ExAbc exabc=ExAbc();
exabc.fun();
exabc.nobita();
}
Now lets move on to implements , whenever we write implements , we have to rewrite all the functions and data members of the superclass .
Ex->
class ImpAbc implements Abc{
@override
int a=20;
@override
void fun(){
print('This is no fun rewriting the same thing');
}
}
void main(){
ImpAbc impabc=ImpAbc();
impabc.fun();
print(impabc.a);
}
Now let’s hop on to with keyword , this is generally used for mixins
Mixins are a way for a class to have functionalities of various other classes , since dart does not support multiple inheritance so we can’t write multiple extends . Thus for a class to inherit functions of other classes we use mixins
// Define a mixin with some functionality
mixin Flying {
void fly() {
print('Flying!');
}
}
// Define another mixin
mixin Swimming {
void swim() {
print('Swimming!');
}
}
// Define a class that uses mixins
class Bird with Flying, Swimming {
void tweet() {
print('Tweeting!');
}
}
void main() {
var bird = Bird();
bird.fly(); // Output: Flying!
bird.swim(); // Output: Swimming!
bird.tweet(); // Output: Tweeting!
}
Now the Bird class can inherit functions of Flying as well as Swimming, thus indirectly we are inheriting functions
Use of abstract classes
Abstract classes are used to define the structure of an entity. We later extend it or implement it as per our usage.
Now how does abstract classes work with the above 3 keywords
abstract class Abc{
int a=10;
void fun(){
print('hello');
}
void roll(){
print('Rolling');
}
}
class ExAbc extends Abc{
int b=20;
void nobita(){
print('hii');
}
@override
void fun(){
print('Bye');
}
}
void main(){
ExAbc exabc=ExAbc();
exabc.nobita();
exabc.roll();
}
Now, extends has the power that it can inherit all the properties from its super class , and we can add new properties and functions to our extended class or change the existing functions by overriding them but there is one catch we inherit and can access the functions of superclass only if it is declared , In abstract classes we have the power to define abstract methods and if we try to access the abstract method without defining it in our child class then we will get a compilation error.
abstract class Abc{
int a=10;
void fun(){
print('hello');
}
void roll(){
print('Rolling');
}
void swim();
}
class ExAbc extends Abc{
int b=20;
void nobita(){
print('hii');
}
@override
void fun(){
print('Bye');
}
}
void main(){
ExAbc exabc=ExAbc();
exabc.nobita();
exabc.roll();
exabc.swim();
}
For implements , we need to implement the entire set of data member and member method , and the best way to use implements is when we use abstract class with abstract methods [ there is also something called abstract interface which would be beyond the scope of this article, but is the best way for implementing it]
abstract class Abc{
int a=10;
void fun(){
print('hello');
}
void roll();
void swim();
}
class ImpAbc implements Abc{
@override
int a=20;
@override
void fun(){
print('This is no fun rewriting the same thing');
}
void roll(){
print('Rolling');
}
void swim(){
print('Swimming');
}
}
void main(){
ImpAbc impabc=ImpAbc();
impabc.fun();
impabc.roll();
impabc.swim();
print(impabc.a);
}
Now here we can see we need to redeclare all the property and functions in our class so the best way is when we have abstract methods , we define abstract class and give a basic structure of how things should look and then later on implement it in another class. [Note : here fun is redundant like we should’nt declare fun in our Abc class, cuz it will be implemented by the class which implements it]
Also a final tip , abstract class can’t be instantiated .
Tadooow!!! You have made it.
Thank You!!!