> ## 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 Override Method

Method overriding is an object-oriented mechanism in Swift that allows a subclass to provide a custom implementation of an instance method or type method inherited from its superclass. By explicitly marking a method with the `override` keyword, the compiler verifies that a matching declaration exists in the superclass chain, ensuring type safety and preventing accidental method shadowing.

## Syntax and Covariant Return Types

To override a method, prefix the `func` declaration with the `override` keyword. The method signature—including the name, parameter labels, and parameter types—must exactly match the superclass implementation.

However, Swift supports **covariant return types**. This means an overridden method in a subclass can return a more specific type (such as a subclass) than the return type defined in the superclass method.

```swift theme={"dark"}
class BaseResult {}
class SpecificResult: BaseResult {}

class BaseClass1 {
    func process() -> BaseResult {
        return BaseResult()
    }
}

class DerivedClass1: BaseClass1 {
    // Valid override utilizing a covariant return type
    override func process() -> SpecificResult {
        return SpecificResult()
    }
}
```

## Accessing the Superclass Implementation

When overriding a method, the subclass implementation completely replaces the superclass implementation for instances of that subclass. To invoke the original superclass behavior within the overridden method, use the `super` prefix.

```swift theme={"dark"}
class BaseClass2 {
    func execute() {
        // Original behavior
    }
}

class DerivedClass2: BaseClass2 {
    override func execute() {
        // Invoke the superclass implementation
        super.execute() 
        
        // Additional subclass-specific behavior
    }
}
```

## Compiler Rules and Constraints

* **Mandatory Keyword:** The `override` keyword is strictly enforced. Attempting to declare a method in a subclass with the identical signature of a superclass method without the `override` modifier results in a compile-time error.
* **Existence Verification:** Applying `override` to a method that does not exist in the superclass (due to a typo or signature mismatch) triggers a compile-time error.
* **Access Control:** The access level of an overridden method is bounded by the access level of the subclass. An overridden method cannot be less accessible than its superclass method, *unless* the subclass itself has a more restrictive access level. For example, an `internal` subclass overriding a `public` method defaults to `internal` access, and explicitly declaring the override as `internal` is perfectly valid. Conversely, a `public` subclass overriding a `public` method must maintain `public` access and cannot restrict the override to `internal`. Additionally, an `open` method can be overridden with a `public` method, which maintains visibility but restricts further overriding outside the defining module.
* **Throwing Methods:** A subclass can override a throwing method with a non-throwing method, but it cannot override a non-throwing method with a throwing method.

## Overriding Type Methods

Swift distinguishes between two types of type-level methods: `static` and `class`. While both are called on the type itself rather than an instance, only methods marked with `class` participate in dynamic dispatch and can be overridden. Methods marked with `static` are implicitly final.

```swift theme={"dark"}
class BaseClass3 {
    class func overridableTypeMethod() { }
    static func staticTypeMethod() { }
}

class DerivedClass3: BaseClass3 {
    override class func overridableTypeMethod() { }
    
    // ERROR: Cannot override static method
    // override static func staticTypeMethod() { } 
}
```

## Preventing Overrides

To explicitly prevent a method from being overridden by any future subclasses, apply the `final` modifier to the method declaration in the superclass. Attempting to override a `final` method results in a compile-time error.

```swift theme={"dark"}
class BaseClass4 {
    final func lockedMethod() {
        // Implementation guaranteed not to change in subclasses
    }
}

class DerivedClass4: BaseClass4 {
    // ERROR: Instance method overrides a 'final' instance method
    // override func lockedMethod() { }
}
```

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