Override Everything

Introduction

Welcome to Lesson 5 of Object Oriented Swift. You will master the word super and override. A lot of beginners have no clue because they simply copy from StackOver Flow and shallow tutorials. I was one of them. That’s no longer acceptable from now on.

Problem

Problem: Super… Super.init?

Example from UIViewController

You might have seen something like this below.

  1. import UIKit
  2. class MyViewController: UIViewController {
  3. override func viewDidLoad() {
  4. super.viewDidLoad()
  5. print("Hello")
  6. }
  7. }

Important: The viewDidLoad() method runs automatically by the UIViewController class.

Many wonder what super and override stand for. Let us find out.

Create Vehicle Class

Create a class called Vehicle. It contains one gettable property and one method.

  1. class Vehicle {
  2. var description: String {
  3. return "Hello, I'm moving at 30km/hr"
  4. }
  5. func warning() {
  6. print("Be careful, I'm going pretty fast")
  7. }
  8. }

Override Method and Property

Create a class called, ToyCar, and inherit Vehicle. You may customize inherited properties and methods by inserting override. You may access inherited properties and methods by calling super.

  1. class ToyCar: Vehicle {
  2. override var description: String {
  3. return "\(super.description) hey, I'm a cute car"
  4. }
  5. override func warning() {
  6. print("hello, don't mind me")
  7. super.warning()
  8. }
  9. }

Now, let us access the description property and the warning method of ToyCar.

  1. let myFutureCar = ToyCar()
  2. myFutureCar.description
  3. // "Hello, I'm moving at 30km/hr hey I'm a cute car"
  4. myFutureCar.warning()
  5. // "hello, don't mind me"
  6. // "Be careful, I'm going pretty fast"

Super Init

You may override an init method from the super class. Remember, every property must be initialized even the ones from the super class.

Design Super Class

Create a class that contain a property called, origin.

  1. class Human {
  2. var origin: String
  3. init(enterOrgin: String) {
  4. origin = enterOrgin
  5. }
  6. }

Design Subclass

Create a subclass, called Korean that inherits from Human. The Korean class contains a property called, name. However, when you initialize, you must initialize the origin property from the Human class by calling super.init.

  1. class Korean: Human {
  2. let city: String
  3. init(enterCity: String) {
  4. self.city = enterCity
  5. super.init(enterOrgin: "Korean")
  6. }
  7. init(enterCity: String, origin: String) {
  8. self.city = enterCity
  9. super.init(enterOrgin: origin)
  10. }
  11. }

Create Object

There are two init methods in the Korean class. You may choose any since both initialize the origin property from the Human class.

  1. let bob = Korean(enterName: "Bob the Dev")
  2. let bobby = Korean(enterName: "Bob the Dev", myOrigin: "Korean")

Override Init

Not only you may override methods and properties, but also init methods.

Design Base Class

Design a class called, Tesla. It contains a property called, numberOfWheels.

  1. class Tesla {
  2. var numberOfWheels: Int
  3. init(enterWheelNumber: Int) {
  4. numberOfWheels = enterWheelNumber
  5. }
  6. }

Design SubClass

Design a called, ModelS that inherits Tesla. Add the override keyword in front of the init method and call super.init to initialize the property from the Tesla class. Once you initialize the property, you may add additional lines of code for customization.

  1. class ModelS: Tesla {
  2. override init(enterWheelNumber: Int) {
  3. super.init(enterWheelNumber: enterWheelNumber)
  4. print("Wow, you've got a nice car")
  5. }
  6. }
  7. ModelS(enterWheelNumber: 50) // Wow, you've got a nice car

Source Code

2005_override_init_method_property.playground

Conclusion

From now on, you no longer have to fear super.init, super.viewDidLoad, and override. If you feel stuck, I recommend you to watch the lecture multiple times and then ask questions if necessary. You’ve got to understand every piece of information from here. If not, you will suffer. A lot.

In the following lesson, you will learn how one init method can initialize the other.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.