Is inheritance different from other languages in swift

superVC:sSuperVC

override func viewDidLoad() {

super.viewDidLoad()

setupUI()

}

override func setupUI() {

print(123)

}


subVC:superVC


verride func viewDidLoad() {

super.viewDidLoad()

}



override func setupUI() {

print(456)

}

subVC()

when push subVC

why its log 456 ,,,,its strange

log info

* frame #0: 0x00000001001abce4 superVC.setupUI(self=0x0000000157ef0620) at superVC.swift:42

frame #1: 0x00000001003a8340 subVC.viewDidLoad(self=0x0000000157ef0620) at subVC.swift:69

frame #2: 0x00000001003a8a9c subVC.viewDidLoad() at <compiler-generated>:0

frame #3: 0x00000001001abc0c superVC.viewDidLoad(self=0x0000000157ef0620) at superVCswift:36

frame #4: 0x00000001001abcb0 superVC.viewDidLoad() at <compiler-generated>:0

Answered by OOPer in 337364022

Please show buildable code.


And if I have fixed your code by guess:

class SuperVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    func setupUI() {
        print(123)
    }
}

class SubVC: SuperVC {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func setupUI() {
        print(456)
    }
}


The method `setupUI()` is overridden in the `SubVC`, so it is as expected that it show 456 as defined in SubVC.

This behavior is consistent with many other Object Oriented language, like Java, Ruby or Objective-C.


Or do you have some experience with C# or C++?

Accepted Answer

Please show buildable code.


And if I have fixed your code by guess:

class SuperVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    func setupUI() {
        print(123)
    }
}

class SubVC: SuperVC {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func setupUI() {
        print(456)
    }
}


The method `setupUI()` is overridden in the `SubVC`, so it is as expected that it show 456 as defined in SubVC.

This behavior is consistent with many other Object Oriented language, like Java, Ruby or Objective-C.


Or do you have some experience with C# or C++?

Is inheritance different from other languages in swift
 
 
Q