Let's say I have two classes: Base class and sub class like this:
class Base{
var name: String?
init() {
name = "The base class"
}
deinit {
println("call Deinitialization in base class")
name = nil
}
}
class Sub: Base{
var subName: String?
init() {
super.init()
subName = "The sub class"
}
deinit {
println("call Deinitialization in sub class")
subName = nil
// does it really call super.deinit() ?
// or assign name = nil ?
}
}
When the deinitializer of sub class is called, does it call super.deinit()
to assign nil to name
variable? Or I have to assign by hand in deinitializer of subclass?
Inheritance allows us to create a new class from an existing class. The new class that is created is known as subclass (child or derived class) and the existing class from which the child class is derived is known as superclass (parent or base class). Swift Inheritance Syntax.
You can prevent a method, property, or subscript from being overridden by marking it as final. Do this by writing the final modifier before the method, property, or subscript's introducer keyword (such as final var , final func , final class func , and final subscript ).
Swift doesn't allow us to declare a class with multiple base classes or superclasses, so there is no support for multiple inheritance of classes. A subclass can inherit just from one class. However, a class can conform to one or more protocols.
Swift - Deinitialization. Before a class instance needs to be deallocated 'deinitializer' has to be called to deallocate the memory space. The keyword 'deinit' is used to deallocate the memory spaces occupied by the system resources. Deinitialization is available only on class types.
Swift - Inheritance. The ability to take than more form is defined as Inheritance. Generally a class can inherit methods, properties and functionalities from another class. Classes can be further categorized in to sub class and super class.
Swift automatically deallocates your instances when they are no longer needed, to free up resources. Swift handles the memory management of instances through automatic reference counting (ARC), as described in Automatic Reference Counting. Typically you don’t need to perform manual cleanup when your instances are deallocated.
A deinitialization process which immediately called before a class instance is deallocated. We can start deinitialization with the deinit keyword. deinit is the predefined keyword to deallocate memory in Swift. Inside deinit, we can write our code.
You can optionally have a deinit
inside your subclass.
If you do
let x = Sub()
you'll see that first the deinit
called is the one inside Sub()
then after, base
deinit
is called. So yes the super.deinit()
is called but after the subclass.
Also the book says (page 286):
You are not allowed to call a deinitializer yourself. Superclass deinitializers are inherited by their subclasses, and the superclass deinitializer is called automatically at the end of a subclass deinitializer implementation. Superclass deinitializers are always called, even if a subclass does not provide its own deinitializer.
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l
Superclass deinitializers are inherited by their subclasses, and the superclass deinitializer is called automatically at the end of a subclass deinitializer implementation. Superclass deinitializers are always called, even if a subclass does not provide its own deinitializer.
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With