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

# Swift Required Initializer

A required initializer is a class initializer marked with the `required` modifier, enforcing a strict compiler contract that every direct and indirect subclass must implement that specific initializer.

## Syntax and Implementation

To designate an initializer as required, place the `required` keyword before the `init` declaration in the superclass. When a subclass implements this initializer, it must also use the `required` keyword to propagate the requirement down the inheritance hierarchy.

When a subclass implements a superclass's already-required initializer, the `override` modifier is omitted.

```swift theme={"dark"}
class BaseClass {
    var name: String
    
    required init(name: String) {
        self.name = name
    }
}

class SubClass: BaseClass {
    var age: Int
    
    // 'required' is mandatory. 'override' is omitted.
    required init(name: String) {
        self.age = 0
        super.init(name: name)
    }
}
```

## Inheritance Rules

Subclasses do not always need to explicitly provide an implementation for a required initializer. If a subclass satisfies Swift's rules for automatic initializer inheritance—specifically, by not defining any new designated initializers of its own—it will implicitly inherit the required initializer from its superclass.

```swift theme={"dark"}
import Foundation

class AnotherSubClass: BaseClass {
    // Inherits required init(name:) automatically 
    // because no new designated initializers are introduced.
    var id: UUID = UUID() 
}
```

## Protocol Conformance

When a class conforms to a protocol that declares an initializer, the compiler mandates that the class implement that initializer with the `required` modifier. This guarantees that any future subclasses will also fulfill the protocol's instantiation requirements.

```swift theme={"dark"}
protocol Describable {
    init(description: String)
}

class Item: Describable {
    var description: String
    
    // Must be 'required' to satisfy the protocol for all potential subclasses
    required init(description: String) {
        self.description = description
    }
}
```

**The `override` and `required` Combination:**
If a subclass satisfies a protocol's initializer requirement by overriding a *non-required* designated initializer from its superclass, both the `required` and `override` modifiers are mandatory.

```swift theme={"dark"}
class SuperItem {
    init(description: String) {
        // Non-required designated initializer
    }
}

class SubItem: SuperItem, Describable {
    var description: String
    
    // Both 'required' and 'override' are mandatory
    required override init(description: String) {
        self.description = description
        super.init(description: description)
    }
}
```

**Exception for Final Classes:**
If a class is marked with the `final` modifier, it cannot be subclassed. Therefore, when a `final` class implements a protocol's initializer, the `required` keyword is omitted because the inheritance chain is terminated.

```swift theme={"dark"}
final class FinalItem: Describable {
    var description: String
    
    // 'required' is omitted because the class is final
    init(description: String) {
        self.description = description
    }
}
```

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