Dart Interfaces

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:-

Example:-

Output:-

dart_interface_example

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:-

Example:-

Output:-

dart_implementing_multiple_interfaces_example

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.

In this tutorial we have learn about the Dart Interfaces and its application with practical example. I hope you will like this tutorial.