An interface in the Dart programming language is considered as a way of creating abstraction within your code. The Dart interface acts as a blueprint for all the classes that wish to implement this interface. Afterwards, these classes are required to provide their own implementations for all the interface methods. In this article, we will teach you the use of the interfaces in the Dart programming language in Ubuntu 20.04.
Main Properties of the Interfaces in the Dart:
Following are the main properties of the interfaces in the Dart programming language:
- There is no direct method of declaring an interface in Dart. However, the simple class declaration can serve as a means of declaring a Dart interface.
- A Dart class can implement multiple interfaces at a time.
- The usage of the interfaces in the Dart programming language can help us in achieving multiple inheritances.
How to use the Interfaces in the Dart in Ubuntu 20.04?
To learn the usage of the interfaces in the Dart programming language, you will have to understand the examples explained below:
Example # 1: A Dart Class Implementing another Dart Class:
Since we know that the Dart programming language does not have a direct way of declaring the interfaces, therefore, in this example, we will be creating a class that implements another class to mimic the functionality of an interface. The following Dart script has been written in this regard:
In this Dart script, we have defined a class named “Organization” which will act as an interface. This class has a single member function named “orgName()”. It will not return any value nor will it accept any input parameters. However, this function only has a “print” statement inside it. After designing this class, we have defined another class named “Employee” that implements the “Organization” class. It means that the “Employee” class must implement all the methods of the “Organization” class along with the rest of its functionality.
Inside the “Employee” class, we have declared a member variable named “empName” that refers to the name of the employee. Then, we have a function named “empInfo()” that is used to print the name of the employee on the console. After that, we have implemented the interface method “orgName()” within the “Employee” class. Then, inside our “main()” function, we have created an instance of the “Employee” class named “E1” and have initialized its “empName” variable by using this instance. Finally, we have called both the methods of this class or its own member function as well as the method of the interface implemented within this class by using its object.
To execute this Dart script, we have used the command given below:
$ dart run interfaces.dart
The output of our first example Dart script is shown in the following image. The most important thing to be noticed in this output is that the message defined in the “orgName()” function of the “Employee” class has been printed on the terminal instead of the present inside the interface class. It means that the “Employee” class has successfully implemented the interface class or the “Organization” class.
Example # 2: The Missing Implementation of the Interface Method in a Dart Class:
This example is just a slightly modified version of our first example. Through this example, we want to teach you what happens when you skip the implementation of any of the interface methods inside the class implementing this interface. To do this, we have used the same Dart script that we have designed for the example above. However, this time, we have commented out the implementation of the “orgName()” function within the “Employee” class as shown in the image below:
The rest of the script is intact. After making this modification, when we tried to execute our Dart script, we received the runtime error shown in the following image:
This error message explicitly states that we have skipped the implantation of an interface method within a class that implements this interface. It means that we cannot execute this Dart script unless we provide an implementation of this interface method within the class that implements this interface. Therefore, it is mandatory to provide the implementations of all the interface methods inside the class that implements a specific interface.
Example # 3: Implementing Multiple Interfaces within a Single Dart Class:
The concept of using the interface in the Dart programming language can also be extended to multiple interfaces. A Dart class can easily implement multiple interfaces at a time. The Dart script shown in the image below has been written to demonstrate this:
In this example, we have defined two interfaces in Dart: Organization and Manager whereas the class “Employee” implements both of these interfaces. The “Organization” class has only one member function named “orgName()”. Similarly, the “Manager” class also has only one member function named “designation()”. The “Employee” class that implements these two interfaces has its own member variable and a member function.
Apart from this, it also implements the two methods of the interfaces “Organization” and “Manager”. Then, in the “main()” function, we have just created an object of the “Employee” class and have accessed all of its member variables and functions with this object.
The output of this Dart script is displayed in the following image. It implies that we have successfully implemented multiple interfaces while using the Dart programming language in Ubuntu 20.04.
With the help of this tutorial, we wanted to explain to you the usage of the interfaces in the Dart programming language. We first began with a brief introduction to the interfaces in Dart followed by a few main properties that are associated with the Dart interfaces. After that, we shared with you three different examples that implemented the Dart interfaces. By going through these examples, you will quickly be able to understand the usage of the interfaces in Dart.