Open In App

Dart - extends Vs with Vs implements

Last Updated : 03 Apr, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

All developers working with Dart for application development using the Flutter framework regularly encounter different usages of the implements, extends, and keywords. In Dart, one class can inherit another class, i.e. , Dart can create a new class from an existing class. We make use of keywords to do so. In this article, we will look into 3 of the keywords used for the same purpose and compare them, namely:

  • extends
  • with
  • implements

Let's look into them one at a time.

 

extends

In Dart, the extends keyword is used for inheritance, allowing a class to inherit properties and methods from another class. The class being inherited from is called the Parent Class (or superclass), while the class that inherits is known as the Child Class (or subclass). For example, if the Apple class extends the Fruit class, it inherits all the properties and methods from Fruit. Additionally, you can override methods in the child class to create more specific functionality.

Example:

Below we can see and example of implementation of the extends keyword. We are not required to override the definition of the inherited class and can use the existing definition in the child class.

Dart
// Class with name First
class First {
    static int num = 1;
    void firstFunc() {
        print('hello');
    }
}

// inherits from First class
class Second extends First {
    // No need to override
}

void main() {
    // instance of First Class
    var one = First();
    
    // calling firstFunc()
    one.firstFunc();
    
    // printing static variable of class
    print(First.num);
    
    // instance of Second Class
    var second = Second();
    
    // calling firstFunc() that
    // has been inherited
    second.firstFunc();
}

 
 Output:

hello
1
hello


implements 

Interfaces in Dart define a set of methods for an object, and class declarations themselves serve as interfaces. An interface requires the implementing class to define specific public fields and methods. The implements keyword forces the redefinition of functions to adhere to the interface. Each class automatically defines an interface that includes its instance members and those of any implemented interfaces. To create a class A that adheres to class B’s API without inheriting its implementation, class A simply implements B’s interface by listing it in an implements clause and providing the necessary APIs.

Example:

Dart
// Class with name First
class First {
    // function to print "hello"
    void firstFunc() {
        print('hello');
    }
}

// We inherit the propertied
// of implemented class
class Second implements First {
    // by overriding the functions
    // in implemented class
    @override
    void firstFunc() {
        print('We had to declare the methods of implemented class');
    }
}

void main() {
    // instance of First Class
    var one = First();
    
    // calling firstFunc()
    one.firstFunc();
    // instance of Second Class
    var second = Second();
    
    // calling firstFunc() that
    // has been inherited
    second.firstFunc();
}

 
Output:

hello
We had to declare the methods of implemented class


with 

Mixins allow for the reuse of a class's methods across multiple class hierarchies, functioning like abstract classes that share similar operations or attributes. They provide a way to abstract and reuse functionality without resorting to multiple inheritance, as only one superclass exists. The keyword with is used to include mixins in Dart, where mixins are defined as classes without constructors. Importantly, mixins don’t enforce type restrictions or usage limitations on class methods.

Example:

Dart
// mixin with name First
mixin First {
    void firstFunc() {
        print('hello');
    }
}

// mixin with name temp
mixin temp {
    void number() {
        print(10);
    }
}

// mixin type used with keyword
class Second with First, temp {
    @override
    void firstFunc() {
        print('can override if needed');
    }
}

void main() {
    var second = Second();
    second.firstFunc();
    second.number();
}


Output:

can override if needed
10


Conclusion

Grasping the use of extends, implements, and with in Dart is key for effective object-oriented programming in Flutter. The extends keyword allows a subclass to inherit and modify features from a superclass, while implements ensures that a class strictly adheres to an interface by requiring it to implement all its methods. The with keyword enables code reuse through mixins, supporting modular design. By utilizing these concepts, developers can create scalable and maintainable Flutter applications that emphasize clean architecture and reusability.


Next Article

Similar Reads