×

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

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 Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

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

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.

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

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 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 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 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 Strings

Strings data type in Dart A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’)...

3 minutes read.

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

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

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

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