Abstract Class in Dart: Learn to Code with Reusability

Home » Dart » Abstract Class in Dart: Learn to Code with Reusability

The abstract class in Dart is a class that cannot be instantiated and may contain abstract methods without implementations.

By defining an abstract class, you can create a common structure and define methods that must be implemented by any subclass. This promotes code reusability and consistency, making it easier to maintain and extend your application.

Abstract Class in Dart: Learn to Code with Reusability

Declare a Class as abstract

To declare an abstract class, use the ‘abstract‘ keyword followed by the class definition.

abstract class School {

 //Abstract methods can be declared here.
}

Abstract Methods in Dart

Abstract methods are declared within abstract classes, indicating that their implementation must be provided by subclasses.

abstract class School {
  void showInfo(); //Abstract method 
}

Program Example:

abstract class School {
  void showInfo(); // Abstract method (no need to explicitly use 'abstract').
  void message() {
    print("Showing a message!"); // Concrete method with def.
  }
}

class Student extends School {
  @override
  void showInfo() {
    print("I am displaying info...");
  }
  
}
void main() {

Student student = Student();
student.showInfo();
student.message();

}

Output

I am displaying info...
Showing a message!

Constructors in Abstract Classes

Abstract classes can have constructors, and subclasses must call these constructors using the super keyword.

abstract class School {
  String? name;

  School(this.name);
}

Program Example :

abstract class School {
 String? name;
 School(this.name);
 void info(); // abstract method
}

class Student extends School
{
    int? id;
    Student(String? name, this.id) : super(name); 
    @override
    void info()
    {
        // implementation for the info method.
        print('School name: $name');
        print('Student Id: $id');
    }
}

void main() {

    Student student = Student('HCKL School',67);
    student.info();
}

School name: HCKL School
Student Id: 67

Multiple Inheritance with Abstract Classes

Unfortunately, Dart doesn’t provide direct support for multiple inheritance. However, it can be achieved using mixins or abstract classes.

Open the DartPad and write the following program:

abstract class Painter {
  void paint();
}

abstract class Draw
{
void draw();
}

class Canvas implements Draw, Painter
{
  @override
  void draw() {
    print('Drawing...');
  }

  @override
  void paint() {
    print('Painting...');
  }

}





void main() {
 Canvas canvas = Canvas();
 canvas.draw();
 canvas.paint();
}

Drawing...
Painting...

Abstract Class with Concrete Methods

Abstract classes can also include concrete methods with implemented functionality.

abstract class Shape {
  void draw(); // Abstract method without implementation.

  void display() {
    print("Displaying the shape.");
  }
}

Why does Flutter use a lot of abstract classes?

Abstract classes allow developers to create customizable and extensible components. Widgets in Flutter, which are the building blocks of the user interface, often use abstract classes to define a base structure that developers can extend and customize according to their needs.

Abstract classes contribute to a consistent and predictable API design across different parts of the Flutter framework. This consistency makes it easier for developers to learn and work with the framework effectively.

Flutter follows the principle of composition over inheritance, encouraging the use of composition and abstract classes to build complex UI structures.

Abstract class in Dart provides a powerful tool for code organization, encapsulation, and promoting code reuse. By understanding and utilizing abstract classes, you can write more modular and maintainable Dart code.

Happy Coding!

You may also like...