result: Stmark(mark1: 3.0, mark2: 3.0))
println("student result is: \(set3.average.m1, set3.average.m2)
\(set3.result.mark1, set3.result.mark2)")
When we run the above program using playground, we get the following result:
(0.0,0.0) (0.0,0.0)
(2.0,2.0) 5.0,5.0)
(2.5,2.5) (3.0,3.0)
Rules for Initializer Delegation
Value Types Class Types
Inheritance is not supported for value
types like structures and enumerations.
Referring other initializers is done
through self.init
Inheritance is supported. Checks all stored
property values are initialized
Class Inheritance and Initialization
Class types have two kinds of initializers to check whether defined stored properties
receive an initial value namely designated initializers and convenience initializers.
Designated Initializers and Convenience Initializers
Designated Initializer Convenience Initializer
Considered as primary initializes for a
class
Considered as supporting initialize for a class
All class properties are initialized and
appropriate superclass initializer are
called for further initialization
Designated initializer is called with
convenience initializer to create class instance
for a specific use case or input value type
At least one designated initializer is
defined for every class
No need to have convenience initializers
compulsory defined when the class does not
require initializers.
Init(parameters) { statements } convenience init(parameters) { statements }
Program for Designated Initializers
class mainClass {
var no1 : Int // local storage
init(no1 : Int) {
self.no1 = no1 // initialization