×

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 use the ' abstract ' keyword to declare the abstract class or method.

But, always remember that it's not necessary for an abstract class to have abstract methods only. However, if a class contains an abstract method, then it is an abstract class only.

Features of Abstract Class:

Here are some of the features of the abstract classes in Dart :

  • One has to only use the ' abstract ' keyword to declare an abstract class. 
  • An abstract class can-not be initialized. 
  • An abstract class can be easily inherited. But, if we inherit an abstract class, we must ensure that all the abstract methods are provided with implementation. 
  • We can not create an object of the abstract class, but it can be extended.

Generally, the abstract classes are built to implement the abstract methods in the inheriting subclasses. 

Given below is the syntax of declaring the abstract class :

abstract class class_name 
{
    // Body of the abstract class
}

Overriding abstract method of an abstract class –

/* definition of the abstract class ' teacher ' that 
 * contains the abstract methods */
abstract class teacher 
{
    /* declaring the abstract class methods in the
     * abstract class ' teacher ' */
      void say( ) ;
      void write( ) ;
}
class student extends teacher
{
    @override
    void say( )
    {
        print( " Overriding the abstract method say( ) of the base class in the derived class ! " ) ;
    }
    @override
    void write( )
    {
        print( " Overriding the abstract method write( ) of the base class in the derived class " ) ;
    }
}
 int main( )
{
    student s1 = new student( ) ;
    s1.say( ) ;
    s1.write( ) ;
  
    return 0 ;
}

Output :

Overriding the abstract method say( ) of the base class in the derived class ! 
Overriding the abstract method write( ) of the base class 

Explanation :

First, we have created an abstract class ' teacher ' that contains two abstract methods ' say( ) ' and ' write( ) ' as its member functions. These functions were overridden in the derived class ' student ' inheriting the abstract class ' teacher '. We defined them to print some messages. After that, in the main( ) function, we created an object of the derived class ' student ' that invokes these functions and generates the respective output.

Note:

It is important to remember that we need not to override the abstract method in the derived class when only that class inherits from the abstract class.

Overriding abstract method of an abstract class in two different classes –

// creating the abstract class ' teacher ' with an abstract method display( )
abstract class teacher
{
    // declaring the abstract method 
    void display( ) ;
}
// class ' student1 ' inheriting the abstract class ' teacher '
class student1 extends teacher 
{
    // Overriding method display( )
    @override
    void display( )
    {
        print( " Overriding the display( ) function of the base class ' teacher ' in the base class ' student1 ' " ) ;
    }
}
 // class ' student2 ' inheriting the abstract class ' teacher '
class student2 extends teacher 
{
    // Overriding method display( ) again 
    @override
    void display( )
    {
        print( " Overriding the display( ) function of the base class ' teacher ' in the base class ' student2 ' " ) ;
    }
}
 void main( )
{
    // creating the object of the class student1 and accessing the display( ) function
    student1 s1 = new student1( ) ;
    s1.display( ) ;
  
    // creating the object of the class student2 and accessing the display( ) function
    student2 s2 = new student2( ) ;
    s2.display( ) ;
}

Output :

 Overriding the display( ) function of the base class ' teacher ' in  
 the base class ' student1 ' 
 Overriding the display( ) function of the base class ' teacher ' in  
 the base class ' student2 ' 

Explanation :

First, we have created an abstract class ' teacher ' that contains an abstract method display( ). This function is overridden in the two derived classes ' student1 ' and ' student2 ' inheriting the abstract class ' teacher '. We defined them to print some messages. After that, in the main( ) function, we created the objects of the derived class ' student1 ' and ' student2 ' that invokes these functions and generates the respective output.


Related Topics

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

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 Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Dart Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

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

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.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute 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 Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 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 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 Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

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