Dart Interfaces

 

Dart Interfaces

An interface defines the syntax that any entity must adhere to. Dart does not have any separate syntax to define interfaces. An Interface defines the same as the class where any set of methods can be accessed by an object. The Class declaration can interface itself.

The keyword implement is needed to be writing, followed by class name to be able to use the interface. Implementing class must provide a complete definition of all the functions of the implemented interface. We can say that a class must define every function with the body in the interface that we want to achieve.

Declaring an Interface

Dart doesn't provide syntax for declaring interface directly. Implicitly, a class declaration itself an interface containing the entire instance member of the class and of any interfaces it implements.

Implementing an Interface

To work with interface methods, the interface must be implemented by another class using the implements keyword. A class which is implemented the interface must provide a full implementation of all the methods that belongs to the interface. Following is the syntax of the implementing interface.

Syntax:

  1. class ClassName implements InterfaceName  

In the following example, we are declaring a class Employee. Implicit, the Engineer class implements the interface declaration for the Employee class. Let's understand the above example by the following code snippet.

Example -

  1. class Employee  
  2. {  
  3.    void display() {  
  4.          print("I am working as an engineer");  
  5.                             }  
  6. }  
  7. // Defining interface by implanting another class  
  8. class Engineer implements Employee   
  9. {  
  10.           void display() {  
  11.                  print("I am an engineer in this company");                   
  12. }  
  13. }  
  14. void main()   
  15. {  
  16. Engineer eng = new Engineer();  
  17. eng.display();  
  18. }  

Output:

I am working as engineer

Explanation

In the above example, we defined a class Engineer as an interface implementing the Engineer class. Then, we defined the same method display() in both classes. This method override in class Engineer, so we created the object of the Engineer class in a main() function invoked the display() function. It printed the output to the screen.

Implementing Multiple Inheritance

We have discussed previously that the multiple inheritance is not supported by the Dart, but we can apply the multiple interfaces. We can say that, using multiple interfaces, we can achieve multiple inheritance in Dart. The syntax is given below.

Syntax:

  1. class ClassName implements interface1, interface2,…interface n  

Let's understand the following example.

Example -

  1. class Student  
  2. {  
  3.    String name;  
  4.    int age;  
  5.      
  6.    void displayName() {  
  7.          print("I am ${name}");  
  8.                             }  
  9.    void displayAge() {  
  10.             print("My age is ${age}");  
  11.                                }  
  12. }  
  13.   
  14. class Faculty  
  15. {  
  16.    String dep_name;  
  17.    int salary;  
  18.      
  19.    void displayDepartment() {  
  20.          print("I am a professor of ${dep_name}");  
  21.                             }  
  22.    void displaySalary() {  
  23.             print("My salary is ${salary}");  
  24.                                }  
  25. }  
  26. // Defining interface by implenting another class  
  27. class College implements Student,Faculty  
  28. {    
  29.    // Overriding the Student class members  
  30.    String name;  
  31.    int age;  
  32.      
  33.    void displayName() {  
  34.          print("I am ${name}");  
  35.                             }  
  36.    void displayAge() {  
  37.             print("My age is ${age}");  
  38.                                }  
  39.   
  40. //Overriding each data member of Faculty class  
  41.    String dep_name;  
  42.    int salary;  
  43.      
  44.    void displayDepartment() {  
  45.          print("I am a proffesor of ${dep_name}");  
  46.                             }  
  47.    void displaySalary() {  
  48.             print("My salary is ${salary}");  
  49.   
  50. }  
  51. }  
  52. void main()   
  53. {  
  54. College cg = new College();  
  55. cg.name = "Handscomb";  
  56. cg.age = 25;  
  57. cg.dep_name = "Data Structure";  
  58. cg.salary = 50000;  
  59.   
  60. cg.displayName();  
  61. cg.displayAge();  
  62. cg.displayDepartment();  
  63. cg.displaySalary();  
  64. }  

Output:

I am Handscomb
My age is 25
I am a professor of Data Structure
My salary is 50000

Explanation:

In the above example, we implemented multiple interfaces in class College. Each data member of Student and Faculty class is overriding in class College. We created the object of College class and invoked the overriding functions. It printed the result.

Rules for Implementing Interfaces

  1. A class that implements the interface must override every method and instance variable of an interface.
  2. Dart doesn't provide syntax to declare the interface directly. The class declaration can consider as the interface itself.
  3. An interface class must provide the full implementation of all the methods belong to the interfaces.
  4. We can implement one or more interfaces simultaneously.
  5. Using the interface, we can achieve multiple inheritance

Comments