Swift Tutorial - Tutorialspoint

(backadmin) #1

}


// Requires only one parameter for convenient method
override convenience init(no1: Int) {

self.init(no1:no1, no2: 0 )

}
}
let res = mainClass(no1: 20 )
let print = subClass(no1: 30 , no2: 50 )

println("res is: \(res.no1)")
println("res is: \(print.no1)")
println("res is: \(print.no2)")

When we run the above program using playground, we get the following result:


res is: 20
res is: 30
res is: 50

Initializer Inheritance and Overriding


Swift does not allow its subclasses to inherit its superclass initializers for their member
types by default. Inheritance is applicable to Super class initializers only to some extent
which will be discussed in Automatic Initializer Inheritance.


When the user needs to have initializers defined in super class, subclass with initializers
has to be defined by the user as custom implementation. When overriding has to be taken
place by the sub class to the super class 'override' keyword has to be declared.


class sides {
var corners = 4
var description: String {
return "\(corners) sides"
}
}
let rectangle = sides()
println("Rectangle: \(rectangle.description)")

class pentagon: sides {
Free download pdf