×

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 another class can have its own additional features as well. In Dart, one class can inherit the properties of another class; this means that a new class can be created from an existing class. This property overcomes the problem of code redundancy.

To inherit a class, we use ‘extends’ keyword.

There are some important terms associated with inheritance :

Parent Class : It is the main class, also known as the base class from which other classes derive some properties. It is sometimes also referred to as the superclass.

Child Class : It is the derived class, also known as the subclass that derives the properties from the parent class and also keeps its own features.

Here is the syntax of inheritance :

class parent_class
{
     // body of the class
}
class child_class extends parent_class
{
    // body of the class
}

Consider the following code in Dart that explains the concept of inheritance:

Example 1 : Single inheritance in Dart 

// Explaining the concept of inheritance using single inheritance
// 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 !

Types of Inheritance :

There are different types of Inheritance in Dart :

  1. Single Inheritance : In this type of inheritance, a child class inherits only a single parent class.
  2. Multiple Inheritance : In this type of inheritance, a child class inherits more than one parent class. However, Dart doesn’t support this.
  3. Multi-Level Inheritance : In this type of inheritance, a child class inherits another child class that inherits another base class.
  4. Hierarchical Inheritance : In this type of inheritance, more than one child classes have the same parent class.

Important Points:

Child classes inherit all the properties and methods except constructors of the parent class.

Like Java, Dart also doesn’t support multiple inheritance.

Consider the following code in Dart that explains the concept of multilevel inheritance:

Example 2:

// Dart program to explain the concept of multilevel inheritance
// This is the definition of the parent class 'teacher'
class teacher
{
  // function member output1( ) 
  void output1( )
  {
    print( ' This is the function of class teacher. All classes have a teacher ! ' ) ;
  }
}
/* This is the definition of the child class ' student ' that
 * inherits the parent class ' teacher ' */
class student extends teacher
{
  // function member output2( )
  void output2( )
  {
    print( ' This is the function of class student. Every student has a teacher ! ' ) ;
  }
}
/* This is the definition of the child class ' classes ' that
 * inherits the parent class ' student ' */
class classes extends student
{
  // function member output3( )
  void output3( )
  {
    print( ' This is the function of class classes. Every class has some students and teacher ! ' ) ;
  }
} 
void main( )
{
  /* creating an object of the derived class ' classes '. This object
   * can access the members of all the classes ' student ', ' teacher '
   * and ' classes ' because ' classes ' class inherits ' teacher ' and ' student ' class.   */
  var c1 = new classes( ) ;
   
  // accessing member function output( ) of ' teacher ' class
  c1.output1( ) ;
   
  // accessing member function display( ) of ' student ' class
  c1.output2( ) ;
  // accessing member function display( ) of ' classes ' class
  c1.output3( ) ;
}

Output : 

This is the function of class teacher. All classes have a teacher !
This is the function of class student. Every student has a teacher !
This is the function of class classes. Every class has some students  and teacher !

Since, here the class ‘classes’ inherit the class ‘student’ which inherits another class ‘teacher’. This is an example of multilevel inheritance.

Consider the following code in Dart that explains the concept of hierarchical inheritance

Example 3 :

// Dart program to explain the concept of hierarchical inheritance
// This is the definition of the parent class ' teacher '
class teacher
{
  // member function output1( )
  void output1( )
  {
    print( ' This is the function of class teacher. All classes have a teacher ! ' ) ;
  }
}
/* This is the definition of the child class ' student ' that
 * inherits the parent class ' teacher ' */
class student extends teacher
{
  // function member output2( )
  void output2( )
  {
    print( ' This is the function of class student. Every student has a teacher ! ' ) ;
  }
}
/* This is the definition of the child class ' classes ' that
 * inherits the parent class ' student ' */
class classes extends teacher
{
  // function member output3( )
  void output3( )
  {
    print( ' This is the function of class classes. Every class has some students and teacher ! ' ) ;
  }
}
void main( )
{
  /* creating the object of the class ' student ' that inherits
   * the class ' teacher ' */
  var s1 = new student( ) ;
   
  // calling the function output1( ) of class ' teacher '
  s1.output1( ) ;
  // calling the function output1( ) of class ' student '
  s1.output2( ) ;
   
  /* creating the object of the class ' classes ' that inherits
   * the class ' teacher ' */
  var s2 = new classes( ) ;
   
  // calling the function output1( ) of class ' teacher '
  s2.output1( ) ;
  // calling the function output3( ) of class ' classes '
  s2.output3( ) ;
 }  

Output :

This is the function of class teacher. All classes have a teacher !
This is the function of class student. Every student has a teacher !
This is the function of class teacher. All classes have a teacher !
This is the function of class classes. Every class has some students and teacher !

If we try to call function output2( ) of class ‘student’ using object s2, the compiler throws an error because the class ‘classes’ inherit the class ‘teacher’ and not ‘student’. Since, here two classes ‘student’ and ‘classes’ have a single parent child ‘teacher’, it is hierarchical inheritance.


Related Topics

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.

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.

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 Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

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

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 Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

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 Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

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

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

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 If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute 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.