Complete and Partial Override Implementation in Swift.

Hello guys,

Let me tell you something regarding overriding which might be helpful to you guys. Basically, override is used within derive class where you want to use parent class’s method. While doing so, you migt want to eiter completely override parent class methods or partially override it. So, what is completely and partially? Ok let me explain with some example.

Completely override: You can override completely in your derive class which when done, your class won’t be able to execute parent class’s method. Doing such will complete ignore parent class’s method and will only run current method mentioned in derived class. Let me give you some example.

class One {
    func viewDidLoad() {
        print("Calling One->viewDidLoad()")
    }
}

class Two: One {
    override func viewDidLoad() {
        print("Calling Two->viewDidLoad()")
    }
}

let two = Two()
two.viewDidLoad()

Result:
Calling Two->viewDidLoad()

In above example, you can see the result as executing print statement of only Two class. This is because Two class’s viewDidLoad is completely overriding viewDidLoad method of parent class One. And this is not allowing class One to execute it’s viewDidLoad() method. This is one kind of technique you can use if it’s something you require within your project.

Partially override: You can partially override parent’s method which actually means you can override parent class’s method by executing parent’s method as well as base class’s method too. This might be helpful if you want to add additional feature which actually a library doesn’t provide or the class you created didn’t provide. Let me give you example using the same code mentioned above to make you understand better.

class One {
    func viewDidLoad() {
        print("Calling One->viewDidLoad()")
    }
}

class Two: One {
    override func viewDidLoad() {
        super.viewDidLoad() //New line of code added.
        print("Calling Two->viewDidLoad()")
    }
}

let two = Two()
two.viewDidLoad()

Result
Calling One->viewDidLoad()
Calling Two->viewDidLoad()

In above code example, I have only added one line of code and that is, super.viewDidLoad(). Even though we override viewDidLoad() method in class Two, since we have called super.viewDidLoad() method, this will execute parent class’s (i.e. One) viewDidLoad() method as well. Thus, we are not completely overriding parent class method. You can call super.viewDidLoad() after print() method as well. Doing so will reverse the execution of print on console as follow:

Calling Two->viewDidLoad()
Calling One->viewDidLoad()

Well, guys.. This is all I wanted to let you know. This is simple thing but can be helpful to someone. So, I decided to post it here. I hope it is helpful.

If you liked it or if this is helpful to someone, please do share it. That would be very helpful. And if you have any queries regarding above example, let me know in the comment down below.

✌️Peace✌️

💻Happy coding💻

Leave a Reply

santoshm