Abstract Classes in Dart: Why Flutter Uses Them Everywhere

Learn abstract classes in Dart with simple explanations, real Flutter-style examples, abstract methods, constructors, inheritance, and best practices. Written from a real teaching perspective.

Introduction

Abstract classes are one of the most important Object-Oriented Programming concepts in Dart.
If you are working with Flutter, you are already using abstract classes every day — even if you don’t realize it.

In this article, we’ll understand what abstract classes are, why they are needed, how they work in Dart, and how Flutter uses them internally, with simple and practical examples.

What Is an Abstract Class in Dart?

An abstract class is a class that cannot be instantiated (you cannot create its object directly).

Its purpose is to:

  • Define a common structure

  • Enforce rules for child classes

  • Act as a base blueprint for other classes

Abstract classes are meant to be extended, not used directly.

Syntax of an Abstract Class

<pre><code>
abstract class Animal {
  void sound();
}
</code></pre>

Key points:

  • The abstract keyword makes the class abstract

  • The method has no body

  • Any class that extends Animal must implement sound()

Why Do We Need Abstract Classes?

Abstract classes are used to:

  • Enforce a fixed structure

  • Avoid incomplete implementations

  • Share common behavior

  • Improve code consistency

  • Build scalable Flutter architectures

In simple words:
Abstract classes define “what must be done”, not “how it is done”.

Abstract Methods in Dart

An abstract method is a method without implementation.


abstract class Shape {
  double area();
}

This means:

  • Every shape must calculate area

  • But each shape calculates it differently

Implementing an Abstract Class

class Rectangle extends Shape {
  double width;
  double height;

  Rectangle(this.width, this.height);

  @override
  double area() {
    return width * height;
  }
}

Here:

  • Rectangle extends Shape

  • It must implement area()

  • Now it becomes a complete class

Important Rule: Object Creation

❌ You cannot create an object of an abstract class:

// Shape s = Shape(); // Not allowed

✔ But you can reference it:

Shape shape = Rectangle(10, 5);
print(shape.area());

This concept is heavily used in Flutter and is part of polymorphism.

Abstract Class with Normal Methods

Abstract classes can have both abstract and concrete methods.

abstract class Vehicle {
  void start(); // abstract method

  void stop() {
    print("Vehicle stopped");
  }
}

Child class:

class Car extends Vehicle {
  @override
  void start() {
    print("Car started");
  }
}

Here:

  • start() must be implemented

  • stop() is reused from parent class

Abstract Class vs Interface in Dart

In Dart:

  • Any class can act as an interface

  • extends → reuse code + override

  • implements → force full implementation

Example using implements:

class Bike implements Vehicle {
  @override
  void start() {
    print("Bike started");
  }

  @override
  void stop() {
    print("Bike stopped");
  }
}

Key difference:

  • No method reuse

  • Every method must be implemented manually

Real Flutter-Style Example

Flutter widgets follow this exact pattern internally.

abstract class Widget {
  void build();
}

Concrete widget:

class Button extends Widget {
  @override
  void build() {
    print("Building Button UI");
  }  

This is why every Flutter widget must override the build() method.

When Should You Use Abstract Classes?

Use abstract classes when:

  • You want to enforce structure

  • Multiple classes share common behavior

  • You are designing frameworks or services

  • You need partial implementation + rules

Avoid abstract classes when:

  • You only need reusable behavior → use mixins

  • You don’t need enforced structure

Common Mistakes

  • Trying to create an object of abstract class

  • Forgetting to override abstract methods

  • Confusing extends with implements

  • Using abstract class instead of mixin

Conclusion

Abstract classes are a core foundation of Dart and Flutter architecture.
They help you write clean, maintainable, and scalable code by enforcing structure and consistency.

Once you understand abstract classes, concepts like interfaces, mixins, and design patterns become much easier.

Share

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0