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

# Dart Abstract Class

An abstract class in Dart is a class declared with the `abstract` modifier that cannot be directly instantiated. It serves as a structural blueprint, allowing developers to define a strict contract of methods and properties that derived classes must implement, while optionally providing shared concrete state and behavior.

## Syntax and Instantiation

To declare an abstract class, prepend the `class` keyword with `abstract`. Attempting to instantiate an abstract class using a generative constructor results in a compile-time error.

```dart theme={"dark"}
abstract class Vehicle {
  // Class body
}

void main() {
  // ERROR: Abstract classes can't be instantiated.
  // Vehicle myVehicle = Vehicle(); 
}
```

## Abstract Methods

Abstract classes can contain abstract methods—method signatures that lack a body (implementation). These are defined by terminating the method signature with a semicolon instead of a code block. Any concrete class extending the abstract class is strictly required to provide an implementation for all inherited abstract methods.

```dart theme={"dark"}
abstract class DataRepository {
  // Abstract methods: No body, ends with a semicolon
  void save(String data);
  String fetch();
}
```

## Concrete Members

Unlike pure interfaces in some other languages, Dart abstract classes can contain concrete state and behavior. They can declare fields, standard methods with bodies, getters, setters, and constructors.

```dart theme={"dark"}
abstract class NetworkClient {
  // Concrete field
  final String baseUrl;

  // Generative constructor
  NetworkClient(this.baseUrl);

  // Abstract method
  void connect();

  // Concrete method
  void disconnect() {
    print('Disconnected from $baseUrl');
  }
}
```

## `extends` vs. `implements`

Because every class in Dart implicitly defines an interface, abstract classes can be utilized in two distinct ways by derived classes:

1. **Using `extends` (Inheritance):**
   The subclass inherits the concrete methods and state of the abstract class. The subclass is only required to override the *abstract* methods. It must also call the abstract class's constructor via `super` if a non-default constructor is defined.

   ```dart theme={"dark"}
   class RestClient extends NetworkClient {
     RestClient(String url) : super(url);

     @override
     void connect() {
       // Implementation required
     }
     // disconnect() is inherited and does not need to be overridden
   }
   ```

2. **Using `implements` (Interface Contract):**
   The subclass treats the abstract class strictly as an interface. It inherits *no* implementation or state. The subclass must override *all* members defined in the abstract class, including concrete methods, getters, setters, and fields.

   ```dart theme={"dark"}
   class MockClient implements NetworkClient {
     // Must override the field
     @override
     final String baseUrl;

     MockClient(this.baseUrl);

     // Must override the abstract method
     @override
     void connect() {}

     // Must override the concrete method
     @override
     void disconnect() {}
   }
   ```

## Factory Constructors in Abstract Classes

While an abstract class cannot be instantiated directly via a generative constructor, it can define a `factory` constructor. This allows the abstract class to return an instance of a concrete subclass when the constructor is invoked, bypassing the instantiation restriction.

```dart theme={"dark"}
abstract class Logger {
  // Factory constructor returning a concrete subclass
  factory Logger() => _ConsoleLogger();

  void log(String message);
}

class _ConsoleLogger implements Logger {
  @override
  void log(String message) => print(message);
}

void main() {
  // Valid: Invokes the factory constructor, returning _ConsoleLogger
  Logger myLogger = Logger(); 
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
