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 Development, Coding Ninjas Studio Problems, Coding Ninjas Studio Interview Bundle, Coding Ninjas Studio Interview Experiences, Coding Ninjas Courses, Coding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog to help other ninjas grow. Happy Coding!