# What do these two key points mean at the end of Chapter 17, Protocols?

**URL:** <https://forums.kodeco.com/t/what-do-these-two-key-points-mean-at-the-end-of-chapter-17-protocols/188670>\
**Category:** Swift Apprentice: Fundamentals\
**Created:** [June 26, 2023, 12:47pm UTC](https://forums.kodeco.com/t/what-do-these-two-key-points-mean-at-the-end-of-chapter-17-protocols/188670 "2023-06-26T12:47:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![1097278366](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/1097278366/32/110937_2.png) [@1097278366](https://forums.kodeco.com/u/1097278366)\
**Post date:** [June 26, 2023, 12:47pm UTC](https://forums.kodeco.com/t/what-do-these-two-key-points-mean-at-the-end-of-chapter-17-protocols/188670/1 "2023-06-26T12:47:15Z")

</div>

- If you create an extension on a protocol that isn’t declared in the protocol, conforming types _cannot_ override the extension.
- If you create an implementation in an extension declared in the protocol, conforming types _can_ override the extension.

---

<div class="post-metadata">

**Author:** ![ehabamer](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ehabamer/32/109011_2.png) [@ehabamer](https://forums.kodeco.com/u/ehabamer)\
**Post date:** [June 26, 2023, 1:07pm UTC](https://forums.kodeco.com/t/what-do-these-two-key-points-mean-at-the-end-of-chapter-17-protocols/188670/2 "2023-06-26T13:07:03Z")

</div>

Hello,  
If you have a function not defined as part of the protocol and is implemented in the protocol extension, any types conforming to this protocol will not be able to override that implementation.  
But if that function is indeed part of the protocol definition, you will be able to override it.

That’s related to dynamic dispatch and static dispatch. if you are interested to understand more about it :]

---

<div class="post-metadata">

**Author:** ![1097278366](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/1097278366/32/110937_2.png) [@1097278366](https://forums.kodeco.com/u/1097278366)\
**Post date:** [June 26, 2023, 3:41pm UTC](https://forums.kodeco.com/t/what-do-these-two-key-points-mean-at-the-end-of-chapter-17-protocols/188670/3 "2023-06-26T15:41:38Z")

</div>

The following code conveys what you mean, right?

```auto
protocol Pro {
    func methodInProtocol()
}

extension Pro {
    func methodInProtocol() {
        print("methodInProtocol by extension")
    }

    func methodNotInProtocol() {
        print("methodNotInProtocol by extension")
    }
}

class Foo: Pro {
    func methodInProtocol() {
        print("methodInProtocol by class")
    }

    func methodNotInProtocol() {
        print("methodNotInProtocol by class")
    }
}

let foo = Foo()
foo.methodInProtocol() // methodInProtocol by class
foo.methodNotInProtocol() // methodNotInProtocol by class

let pro: Pro = Foo()
pro.methodInProtocol() // methodInProtocol by class
pro.methodNotInProtocol() // methodNotInProtocol by extension

```

---

<div class="post-metadata">

**Author:** ![ehabamer](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ehabamer/32/109011_2.png) [@ehabamer](https://forums.kodeco.com/u/ehabamer)\
**Post date:** [June 26, 2023, 6:35pm UTC](https://forums.kodeco.com/t/what-do-these-two-key-points-mean-at-the-end-of-chapter-17-protocols/188670/4 "2023-06-26T18:35:54Z")

</div>

Exactly yes.  
by adding the function to the protocol definition, dynamic dispatch is used. that’s why even when u treat the variable using its protocol type, the function in the class is still being called.  
If not added, static dispatch is used and the function specified in the extension will be called and not the class.

---

<div class="post-metadata">

**Author:** ![1097278366](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/1097278366/32/110937_2.png) [@1097278366](https://forums.kodeco.com/u/1097278366)\
**Post date:** [June 27, 2023, 2:57am UTC](https://forums.kodeco.com/t/what-do-these-two-key-points-mean-at-the-end-of-chapter-17-protocols/188670/5 "2023-06-27T02:57:38Z")

</div>

Thank you for answering my question.

---

<div class="post-metadata">

**Author:** ![rayfix](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rayfix/32/108995_2.png) [@rayfix](https://forums.kodeco.com/u/rayfix)\
**Post date:** [June 27, 2023, 8:14pm UTC](https://forums.kodeco.com/t/what-do-these-two-key-points-mean-at-the-end-of-chapter-17-protocols/188670/6 "2023-06-27T20:14:48Z")

</div>

Pop quiz… just for fun. 😁 What will the following print?

```swift
protocol Drawable {
  func draw()
}

extension Drawable {
    func draw() {
        print("Base draw()")
    }
}

extension Drawable {
   func drawCircle() {
      print("Base drawCircle()")
   }
}

func render(shape: any Drawable) {
  shape.draw()
  shape.drawCircle()
}

struct Square: Drawable {
    func draw() {
        print("Square draw")
    }
    func drawCircle() {
        print("Square drawCircle()")
    }
}

render(shape: Square())

```

---

<div class="post-metadata">

**Author:** ![1097278366](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/1097278366/32/110937_2.png) [@1097278366](https://forums.kodeco.com/u/1097278366)\
**Post date:** [June 28, 2023, 4:31am UTC](https://forums.kodeco.com/t/what-do-these-two-key-points-mean-at-the-end-of-chapter-17-protocols/188670/7 "2023-06-28T04:31:54Z")

</div>

```auto
//Square draw
//Base drawCircle()

```

Verified in Xcode

---

<div class="post-metadata">

**Author:** ![rayfix](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rayfix/32/108995_2.png) [@rayfix](https://forums.kodeco.com/u/rayfix)\
**Post date:** [June 28, 2023, 5:10am UTC](https://forums.kodeco.com/t/what-do-these-two-key-points-mean-at-the-end-of-chapter-17-protocols/188670/8 "2023-06-28T05:10:49Z")

</div>

Righteo. 👍 Sorry if that was obvious. 😅
