DART INTERFACE - IMPLEMENTS KEYWORD - DART FOR FLUTTER TUTORIALS

113 Просмотры
Издатель
Dart INTERFACE ( implements keyword ) Example. Dart Programming for Flutter

Dart beginners tutorial. Learn how to implement an Interface in Dart language. Since dart has no direct keyword of 'interface' so we need to follow some rules to make a normal class to act like an Interface. Also, learn how to override method while implementing an Interface.


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.

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.

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

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

#Dart
#Exceptions
#ntegerDivisionByZeroException
#IOException
#DeferredLoadException
#Timeout
#IsolateSpawnException
#Programmin
#try-catch-finally
#Finally
#app studio
#development tutorials
#app builder
#tutorials
#web
#apps
android tutorials
#android studio tutorial
#flutter error
#fix flutter error
#flutter tutorial
#fixed flutters issues
#gradle tasks fixed
#Flutter fix tasks
#flutter tutorial 2021
#flutter 2 tutorial 2021
#flutter full tutorial 2021
#flutter app tutorial 2021
#flutter app fixed errors










SOUCE CODE: https://github.com/benjilo/Dart-Interfaces/blob/main/Dart%20Interface.dart
Категория
Язык программирования Dart
Комментариев нет.