Table of contents
1.
Introduction
2.
Dart Interfaces
2.1.
Declaration of Dart Interface 
3.
Implementation of Interface
4.
Multiple Inheritance
5.
Frequently Asked Questions
5.1.
Can an interface be inherited in more than one implementing class?
5.2.
What is the difference between an abstract class and an interface?
5.3.
Is it possible for the implementing class to not implement some of the methods of its associated interface?
6.
Conclusion 
Last Updated: Mar 27, 2024

Dart Interfaces

Author Pradeep Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into Dart Interfaces. We will see how an interface is defined and how its characteristics can be overridden in its implementing types. If you are new to Dart and don't know how classes are instantiated and used in Dart, you can check out our other article on Dart Classes and Objects

Dart Interfaces

In Dart, Interfaces are custom types that describe a pattern of behavior and characteristics. 

If a class inherits an interface, it should redefine each function present inside an interfaced class in its way. 

Declaration of Dart Interface 

We must use the implements keyword to construct an inherited class because Dart doesn't offer a simple mechanism to do so.

// Interface
class InterfaceClass {
  // body of the interface class
}

// Class implementing the interface
class ImplementingClass implements InterfaceClass {
  // body of the implementing class
}

Implementation of Interface

In Dart, an interface can be implemented by a class. The class that implements an interface must define all of the interface's methods. You can simply write the class name followed by the keyword implements and then the interface's name to implement an interface in a class. For example,

class Red implements Colour
Example
// Interface
class Colour {
  void printColour() {
    print("This is the interface.");
  }
}

// Class implementing the interface
class Red implements Colour {
  void printColour() {
    print("This is the Red class, implementing the Colour Interface.");
  }
}

// Main function to run the code
void main() {
  // create an object of the red class
  Red red = new Red();

  // invoking the printColour function for the Red class
  red.printColour();
}

Output:

This is the Red class, implementing the Colour Interface.

Here, we created an interface Colour which was then implemented by the Red Class. The Red class provided the implementation of the printColour function. 

Multiple Inheritance

Dart also allows for a class to implement multiple interfaces. It can be done by using the implements keyword followed by the name of interfaces separated by commas. For example,

// Interface for division
class Division {
  void divide(int a, int b) {
    print("Default Implementation of the divide method.");
  }
}

// Interface for multiplication
class Multiplication {
  void multiply(int a, int b) {
    print("Default Implementation of the multiply method.");
  }
}

// Interface for subtraction
class Subtraction {
  void subtract(int a, int b) {
    print("Default Implementation of the subtract method.");
  }
}

// Interface for addition
class Addition {
  void add(int a, int b) {
    print("Default Implementation of the add method.");
  }
}

class Calculator implements Addition, Subtraction, Multiplication, Division {
  // Implementation of the add method of the Addition Interface
  void add(int a, int b) {
    print("$a + $b = ${a + b}");
  }

  // Implementation of the subtract method of the Subtraction Interface
  void subtract(int a, int b) {
    print("$a - $b = ${a - b}");
  }

  // Implementation of the multiply method of the Multiplication Interface
  void multiply(int a, int b) {
    print("$a * $b = ${a * b}");
  }

  // Implementation of the divide method of the Division Interface
  void divide(int a, int b) {
    // check whether or not the divisor is zero?
    if (b == 0) {
      // Invalid divisor
      print("Invalid operation. Can't divide by zero.");
    } else {
      // Valid divisor
      print("$a/ $b = ${a / b}");
    }
  }
}

// Main function to run the program
void main() {
  // Creating an object of the Calculator class
  Calculator calculator = new Calculator();

  // invoking the add method
  calculator.add(3, 5);
 
  // invoking the subtract method
  calculator.subtract(25, 3);

  // invoking the multiply method
  calculator.multiply(3, 5);

  // invoking the divide method
  calculator.divide(25, 5);

}

Output:

3 + 5 = 8
25 - 3 = 22
3 * 5 = 15
25/ 5 = 5.0

Here, the class Calculator is inheriting the interfaces Addition, Subtraction, Multiplication, and Division. The class Calculator implementing interfaces overrides all the methods defined in the Addition, Subtraction, Multiplication, and Divison class.

Frequently Asked Questions

Can an interface be inherited in more than one implementing class?

Yes, any number of classes can implement an interface. All those classes can have different implementations for the methods of the interface.

 

What is the difference between an abstract class and an interface?

An abstract class, which is similar to an interface, defines a class that cannot be instantiated.

 

Is it possible for the implementing class to not implement some of the methods of its associated interface?

No, the implementing type has to provide the implementation of all the methods defined in its interface.

Conclusion 

In this article, we have extensively discussed the Interfaces in Dart. We hope that this blog has helped you enhance your knowledge regarding the Dart Interfaces, and to learn more, check out our other article on Dart Abstract Classes. And also check out the awesome content on the Coding Ninjas Website,
Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass