iOS Interview: Method Dispatch (Dynamic_Dispatch & Static Dispatch)

Ravi Ranjan
CodeX
Published in
2 min readDec 11, 2022

I have a very fundamental question for all the readers who want to understand the Method dispatch (Dynamic_Dispatch & Static Dispatch).

Method dispatch is the process of selecting the implementation of a method to be executed at runtime. In object-oriented languages like Swift, method dispatch is used to support polymorphism, which allows objects of different classes to respond to the same method call in different ways.

There are two main types of method dispatch: dynamic dispatch and static dispatch.

Dynamic dispatch is a type of method dispatch in which the implementation of a method is selected at runtime based on the type of object that is receiving the method call. Dynamic dispatch is also known as late binding or runtime binding because the method implementation is not bound to the method call until runtime.

Dynamic dispatch is often used in languages that support inheritance and polymorphism because it allows objects of different classes to respond to the same method call in different ways. In dynamic dispatch, the method implementation that is selected at runtime is determined by the type of object that is receiving the method call, not by the type of object that is calling the method.

class Animal {
func makeNoise() {
// Default implementation
}
}

class Dog: Animal {
override func makeNoise() {
print("Woof!")
}

If you liked this, click the 💚 and give a clap on this post as much as you can below so other people will see this here on Medium. If you have any queries or suggestions, feel free to comment or hit me on Twitter, or Linkedin.

--

--