Dart Basic- Interfaces

What is interface


An interface defines the syntax that any entity must adhere to. Interfaces define a set of methods available on an object. Dart does not have a syntax for declaring interfaces. Class declarations are themselves interfaces in Dart.

Classes should use the implements keyword to be able to use an interface. 

Example:

In the following program, we are declaring a class Printer. The ConsolePrinter class implements the implicit interface declaration for the Printer class. The main function creates an object of the ConsolePrinter class using the new keyword. This object invokes the function print_data defined in the ConsolePrinter class.

void main() {
   ConsolePrinter cp= new ConsolePrinter();
   cp.print_data();
}  
class Printer {
   void print_data() {
      print("__________Printing Data__________");
   }
}  
class ConsolePrinter implements Printer {
   void print_data() {  
      print("__________Printing to Console__________");
   }
}

The output will be:

__________Printing to Console__________

In Dart, an interface is a contract that describes the behavior or capabilities of a class. It defines a set of abstract methods and/or properties that a class must implement in order to be considered as implementing the interface.

Interface Basics


In Dart, interfaces are implemented using the implements keyword. To implement an interface, a class must define all of the methods and/or properties that are declared in the interface. Here is an example:

abstract class Animal {
    void makeSound();
}
class Dog implements Animal {
    @override
    void makeSound() {
        print("Woof!");
    }
}

void main() {
    var dog = Dog();
    dog.makeSound(); // prints "Woof!"
}


In the above example, we define an interface Animal that declares an abstract method makeSound(). We then define a class Dog which implements the Animal interface and provides an implementation for the makeSound() method.

Note that the Animal class is declared as abstract. This means that it cannot be instantiated directly, but can only be used as a base class for other classes that implement its interface.

Interface Inheritance


Interfaces in Dart can also inherit from other interfaces. This allows for creation of more specialized interfaces that extend the behavior of more general ones. Here is an example:

abstract class Animal {
    void makeSound();
}
abstract class Mammal extends Animal {
    void nurse();
}

class Dog extends Mammal {
    @override
    void makeSound() {
        print("Woof!");
    }

    @override
    void nurse() {
        print("I'm nursing my puppies.");
    }
}

void main() {
    var dog = Dog();
    dog.makeSound(); // prints "Woof!"
    dog.nurse(); // prints "I'm nursing my puppies."
}


In this example, we define a more specialized interface Mammal that extends the Animal interface and declares an additional abstract method nurse(). We then define a class Dog which implements the Mammal interface and provides implementations for both the makeSound() and nurse() methods.

Using Interfaces


Interfaces are commonly used in Dart to define contracts between different program parts. For example, if you are building a library that provides a set of common operations that can be performed on different types of objects, you might define an interface that all of the objects must implement in order to be compatible with your library.

Here is an example:

abstract class Database {
    void connect();
    void disconnect();
    List> query(String sql);
}
class MySQLDatabase implements Database {
    @override
    void connect() {
        // connect to MySQL server
    }

    @override
    void disconnect() {
        // disconnect from MySQL server
    }

    @override
    List> query(String sql) {
        // execute SQL query on MySQL server and return results
        return [];
    }
}

void main() {
    var db = MySQLDatabase();
    db.connect();
    var results = db.query("SELECT * FROM users");
    db.disconnect();
}


In this example, we define an interface Database that declares three abstract methods: connect(), disconnect(), and query(). We then define a class MySQLDatabase which implements the Database interface and provides implementations for all three methods.

By defining an interface in this way, we can write code that operates on Database objects without knowing anything about the specific implementation of the Database class. This makes our code more modular and easier to maintain.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.