Object-Oriented

Dart Interfaces

Any entity must follow the syntax defined by an interface. There is no distinct syntax in Dart for defining interfaces. An interface is defined as the class that allows an object to access any set of methods. Class declarations are self-interfacing.

To utilize the interface, you must use the term implement first, then the class name. A thorough description of every function of the implemented interface must be provided by the implementation class. It is possible to claim that any function that we wish to accomplish with the interface’s body needs to be defined by a class.

Declaring an Interface

The syntax for directly specifying an interface is not provided by Dart. A class declaration implicitly defines an interface that includes every instance member of the class as well as any interfaces it implements.

Implementing an Interface

The implements keyword must be used by another class to implement the interface in order to use interface methods. All of the methods included in the interface must be fully implemented by the class that implements it. This is the implementing interface’s syntax.

Syntax

				
					class ClassName implements InterfaceName  
				
			

We are declaring a class Employee in the example that follows. It is implied that the interface definition for the Employee class is implemented by the Engineer class. With the help of the following code sample, let’s comprehend the example above.

Example

				
					class Employee  
{  
   void display() {  
         print("I am working as an engineer");  
                            }  
}  
// Defining interface by implanting another class  
class Engineer implements Employee   
{  
          void display() {  
                 print("I am an engineer in this company");                   
}  
}  
void main()   
{  
Engineer eng = new Engineer();  
eng.display();  
}  
				
			

Output

				
					I am working as engineer
				
			

Explanation

An interface that implements the Engineer class is what we specified as the class Engineer in the example above. Next, we declared the display() method 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

As we previously established, Dart does not enable multiple inheritance; nevertheless, several interfaces can be used. We may say that multiple inheritance is possible in Dart by utilizing several interfaces. Below is the syntax.

Syntax

				
					class ClassName implements interface1, interface2,…interface n  
				
			

Example

				
					class Student  
{  
   String name;  
   int age;  
     
   void displayName() {  
         print("I am ${name}");  
                            }  
   void displayAge() {  
            print("My age is ${age}");  
                               }  
}  
  
class Faculty  
{  
   String dep_name;  
   int salary;  
     
   void displayDepartment() {  
         print("I am a professor of ${dep_name}");  
                            }  
   void displaySalary() {  
            print("My salary is ${salary}");  
                               }  
}  
// Defining interface by implenting another class  
class College implements Student,Faculty  
{    
   // Overriding the Student class members  
   String name;  
   int age;  
     
   void displayName() {  
         print("I am ${name}");  
                            }  
   void displayAge() {  
            print("My age is ${age}");  
                               }  
  
//Overriding each data member of Faculty class  
   String dep_name;  
   int salary;  
     
   void displayDepartment() {  
         print("I am a proffesor of ${dep_name}");  
                            }  
   void displaySalary() {  
            print("My salary is ${salary}");  
  
}  
}  
void main()   
{  
College cg = new College();  
cg.name = "Handscomb";  
cg.age = 25;  
cg.dep_name = "Data Structure";  
cg.salary = 50000;  
  
cg.displayName();  
cg.displayAge();  
cg.displayDepartment();  
cg.displaySalary();  
}  
				
			

Output

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

Explanation

We implemented numerous interfaces in the College class in the example above. Every student and faculty member’s data is overriding in the college class. We generated the College class object and called the overriding methods. The outcome was printed.

Rules for Implementing Interfaces

1.Every method and instance variable of an interface must be overridden by the class that implements it.
2.The syntax needed to declare an interface directly is not provided by Dart. You can think of the class definition as the interface itself.
3.Every method that is part of an interface must have a complete implementation provided by an interface class.
4.One or more interfaces can be implemented at the same time.
5.Multiple inheritance is possible using the interface.

Share this Doc

Dart Interfaces

Or copy link

Explore Topic