×

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 Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

3 minutes read.

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.

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 Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 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 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.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Single-Page Application Architecture

In a single page application architecture, the source code for a single web page loads the entire application. The responsibility of building the user interface and requesting data from the...

2 minutes read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

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.