In this tutorial you will learn about the Dart Interfaces and its application with practical example.
Dart Interfaces
In Dart, we can think of an interface as a blueprint of a class; that any class entity must adhere to. Interfaces declares a set of methods available on an object. An interface can have methods and variables just like a class but in interface only abstract declaration of method is provided. Unlike classes, an interface can only contain method signatures, there is no full body implementation of the methods is provided. In order to use interface methods, the interface must be implemented by another class. Class should use the implements keyword (instead of extends) to be able to use an interface method. A class implementing interface must provide a concrete implementation of all the methods belongs to the interface. In other words, a class must redefine every method of the interface it is implementing.
Implicit interfaces
In Dart, every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. A class is allowed to implement one or more interfaces by declaring them in an implements clause and then providing the APIs required by the interfaces.
Usage of Interfaces
In Dart, interfaces is a way to achieve full abstraction. Since interface methods do not have body, the class that implements interface must implement all the methods of that interface before it can be accessed.
1.) An Interface is used to achieve abstraction.
2.) Interface is a mechanism to achieve multiple inheritance in Dart.
Declaring an Interface
In Dart, there is no way does for declaring interfaces directly. In Dart, class declarations themselves implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. .
Implementing an Interface
In order to use interface methods, the interface must be implemented by another class. Class should use the implements keyword (instead of extends) to be able to use an interface method. A class implementing interface must provide a concrete implementation of all the methods belongs to the interface. In other words, a class must redefine every method of the interface it is implementing.
Syntax:-
1 |
class ClassName implements InterfaceName |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Employee { void showEmpInfo(){ print("I Am Employee"); } } class Manager implements Employee { @override void showEmpInfo(){ print("I Am Manager"); } } void main(){ Manager mng = new Manager(); print("W3Adda - Dart Interface Example."); mng.showEmpInfo(); } |
Output:-
Implementing Multiple Interface
As we know the Dart does not support multiple inheritance, but a class can implement multiple interfaces. This way Interface can be used as a mechanism to achieve multiple inheritance in Dart.
Syntax:-
1 |
class ClassName implements interface1, interface2, interface3... |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
class Person { String name; int age; void ShowName() { print("My name is $name"); } void ShowAge() { print("My Age is $age"); } } class Profession { String prof; int salary; void ShowProfession() { print("My Profession Is: $prof"); } void ShowSalary() { print("My Salary Is: $salary"); } } class Employee implements Person, Profession { @override String name; @override int age; @override void ShowName() { print("Employee Name Is: $name"); } @override void ShowAge() { print("Employee Age Is: $age"); } @override String prof; @override int salary; @override void ShowProfession() { print("Employee Profession Is: $prof"); } @override void ShowSalary() { print("Employee Salary Is: $salary"); } } main() { Employee emp = new Employee(); emp.name = "Keith"; emp.age = 30; emp.prof = "System Analyst"; emp.salary = 25000; print("W3Adda - Dart implementing Multiple Interfaces Example"); emp.ShowName(); emp.ShowAge(); emp.ShowProfession(); emp.ShowSalary(); } |
Output:-
Rules For Implementing An Interfaces
1.) Any class implement an Interface must override every method and instance variable of an interface.
2.) In Dart, there is no syntax for declaring an interfaces, class declaration itself implicitly defines an interface.
3.) A class implementing interface must provide a concrete implementation of all the methods belongs to the interface.
4.) A class can implement more than one interface simultaneously.
5.) A class can extend only one class, but can implement multiple interfaces.