×

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is one class can inherit another class. Dart can create a new class from an existing class. To implement such features in Dart we make use of keywords to do so. In this article, we will look at 3 keywords used for the same purpose and compare them, namely:

  • extends
  • with
  • implements

Let’s look into them one at a time.

Dart – extends, with, implements Keywords

extends

In Dart, an extended keyword is often used to change class behaviour using Inheritance. The ability of a class to acquire properties and features in another category is called Inheritance. It is the ability of a program to create a new class from an existing class. In simple terms, we can say that we use extends to form a subclass, and super referring to the superclass. A class whose properties the child class inherits is called a Parent Class. The parent class is also known as the base class or super class.

The class that inherits properties from another class is called child class. Child class is also known as derived class, heir class, or subclass.

The extends keyword is the typical OOP class inheritance. If a class ‘A’ extends another class ‘ B ’ then all the properties, variables, and methods implemented in the parent class ‘ A ’ are also available in the child class ‘ B ’. Moreover, we can also override the functions of the base class in the derived class.

We use the keyword ‘extends’ if you want to create a more specific version of a class. For instance, if a child class ‘Car’ extends from the base class ‘ Vehicle ’, then all the properties, variables and functions defined in the ‘ Car ’ class will be available in the ‘ Vehicle ’ class.

Example :

Let us consider an example of implementation of the ‘extends’ keyword. There is no need to override the definition of the inherited class and can use the existing definition in the child class.

// This is the definition of the parent class ' teacher’
class teacher
{
  // function member output( ) 
  void output( )
  {
    print( " All classes have a teacher ! " ) ;
  }
}
  
/* This is the definition of the base class ' student ' that inherits
 * the base class ' teacher ' */
class student extends teacher
{
  // function member output( ) 
  void display( )
  {
    print( " Every student is taught by some teacher ! " ) ;
  }
}
void main( ) 
{
  /* creating an object of the derived class ' student '. This object
   * can access the mmebers of both the classes ' student ' and ' teacher '
   * because ' student ' class inherits ' teacher ' class. */
  var s1 = new student( ) ;
    
  // accessing member function output( ) of ' teacher ' class
  s1.output( ) ;
  
  // accessing member function display( ) of ' student ' class
  s1.display( ) ;
} 

Output :

All classes have a teacher! 
Every student is taught by some teacher!

implements

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the user with the blueprint of the class that any class should follow if it interfaces with that class. If a class inherits another class, it should override ( re-define ) the functions present inside that interfaced class in its own way as per the subject. In Dart, there isn't a specific or direct way of creating the interfaces. To implement them, we use the ' implement ' keyword. By default, every class is an interface in itself containing all the interface members and the members of any other interfaces that it implements.

Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface. A class implements one or more interfaces by declaring them in an implements clause and then providing the APIs required by the interfaces.

Example 2:

// definition of the interface class ' Interface1'
class Interface1
{ 
   // definition of the display1( ) function of the ' Interface1 ' class
   void display1 ( ) 
   { 
      print( " This is the function of the interface class ' Interface1 '. " ) ; } 
}  
// definition of the interface class ' Interface2 '
class Interface2 
{ 
  // definition of the display2( ) function of the ' Interface2 ' class 
  void display2( ) 
   { 
      print( " This is the function of the interface class ' Interface2 '. " ) ; } 
}  
// definition of the interface class ' Interface3’
class Interface3 
{ 
  // definition of the display3( ) function of the ' Interface2 ' class 
  void display3( ) 
   { 
      print( " This is the function of the interface class ' Interface3 '. " ) ; 
   } 
}  
// definition of the subclass implementing the above three interface classes
class subclass implements Interface1, Interface2, Interface3 
{ 
   void display1( ) 
   {  
      print(" Overriding the display1( ) function in the subclass. " ) ; 
   }  
  void display2( ) 
   {  
      print( " Overriding the display2( ) function in the subclass. " ) ; 
   } 
   void display3( ) 
   {  
      print ( " Overriding the display3( ) function in the subclass. " ) ; 
   }   
} 
void main( )
{
   // creating the object of the subclass
   subclass s1 = new subclass( ) ;
    // calling overridden methods of the interface classes in the subclass
   s1.display1( ) ; 
   s1.display2( ) ; 
   s1.display3( ) ; 
}  

Output :

Overriding the display1( ) function in the subclass. 
Overriding the display2( ) function in the subclass. 
Overriding the display3( ) function in the subclass. 

with

Mixins are a way to reuse class methods in multiple class hierarchies. Mixins can be understood as abstract classes that are used to reuse methods in different classes with the same functions / attributes. Mixins are a way of thinking and reusing the working the family of operations and state. It is similar to the reuse you get from extending a class, but not multiple inheritances. There is still only one superclass.

The ' with ' keyword is used to install Mixins. Mixin is a different type of structure, which can only be used with a keyword ' with '.

Mixin is a different type of structure, which can only be used with a keyword ' with '. In Dart, the class can play the role of mixin if the class does not have a constructor. It is also important to note that mixin does not impose a limit type and imposes restrictions on use in class methods.

Example 3:

// mixin with name ' teacher '
mixin teacher {
  void func( ) {
    print( ' This is the function of the mixin teacher ' ) ;
  }
}
// mixin with name ' student ' 
mixin student { 
  void func2( ){
    print( 125 ) ;
  }
}
// mixin type used with keyword
class Principal with teacher, student{ 
  @override
  void func( ) {
    print( ' We can override function inside this class if needed ' ) ;
  }
}
void main( ) {
  var princi = Principal( ) ;
  princi.func( ) ;
  princi.func2( ) ;
}

Output :

We can override function inside this class if needed 
125

Related Topics

Dart Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

5 minutes read.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes read.

Dart Packages

Every programming language has some in-built functions stored inside the header files that makes the task much easier for the programmer. The Dart Packages refer to the compilation of a...

4 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Static Members

Static Members in Dart Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :  All the objects of the class having...

3 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart Future and Stream

Before understanding Future and Stream in Dart, we need first to get familiar with two terms : Synchronous and Asynchronous Programming.  In synchronous programming, a single operation is handled at a...

9 minutes read.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.