Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An abstract base class in Dart is a class declared with the abstract modifier that cannot be directly instantiated. It serves as a structural blueprint, defining a contract of method signatures and properties that concrete subclasses must implement, while optionally providing shared concrete implementations.

Syntax and Declaration

To define an abstract class, precede the class keyword with the abstract modifier. Abstract methods within the class are defined by terminating the method signature with a semicolon instead of providing a method body.
abstract class AbstractBase {
  // Instance variable
  String baseProperty;

  // Constructor
  AbstractBase(this.baseProperty);

  // Abstract method: Signature only, no body
  void abstractMethod();

  // Concrete method: Fully implemented
  void concreteMethod() {
    print('Executing concrete method. Property: $baseProperty');
  }
}

Core Technical Rules

1. Instantiation Restriction Attempting to instantiate an abstract class directly results in a compile-time error.
// Compile-time error: Abstract classes can't be instantiated.
var instance = AbstractBase('value'); 
2. Subclassing (extends) When a concrete class extends an abstract base class, it inherits all concrete members and must provide implementations for all abstract methods.
class ConcreteSubclass extends AbstractBase {
  ConcreteSubclass(String baseProperty) : super(baseProperty);

  // Must override the abstract method
  @override
  void abstractMethod() {
    print('Implemented abstract method');
  }
}
3. Implicit Interfaces (implements) Like all classes in Dart, an abstract class implicitly defines an interface. If a class implements an abstract class rather than extending it, it must provide implementations for all members of the abstract class, including concrete methods and instance variables.
class InterfaceImplementation implements AbstractBase {
  // Must implement the instance variable
  @override
  String baseProperty;

  InterfaceImplementation(this.baseProperty);

  // Must implement the abstract method
  @override
  void abstractMethod() {
    print('Implemented abstract method');
  }

  // Must ALSO implement the concrete method
  @override
  void concreteMethod() {
    print('Re-implemented concrete method');
  }
}
4. Abstract Subclasses A class that extends an abstract base class can itself be declared abstract. In this scenario, the subclass is not required to implement the inherited abstract methods; the implementation burden is deferred to the next concrete subclass in the inheritance hierarchy.
abstract class IntermediateAbstract extends AbstractBase {
  IntermediateAbstract(String baseProperty) : super(baseProperty);
  
  // abstractMethod() remains unimplemented here
  
  void additionalAbstractMethod();
}
5. Factory Constructors While an abstract class cannot be instantiated directly, it can define a factory constructor that returns an instance of a concrete subclass. This allows the abstract class to act as an entry point for instantiation logic without violating the instantiation restriction.
abstract class AbstractWithFactory {
  factory AbstractWithFactory() {
    return ConcreteImplementation();
  }
  
  void doWork();
}

class ConcreteImplementation implements AbstractWithFactory {
  @override
  void doWork() {}
}
Master Dart with Deep Grasping Methodology!Learn More