Swift Tutorial - Tutorialspoint

(backadmin) #1

}


}


let rect = Rectangle()
rect.radius = 25.0
rect.print = 3
println("Radius \(rect.area)")

class Square: Rectangle {
override var radius: Double {
didSet {
print = Int(radius/5.0)+ 1
}
}
}

let sq = Square()
sq.radius = 100.0
println("Radius \(sq.area)")

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


Radius of rectangle for 25.0 is now overridden as 3
Radius of rectangle for 100.0 is now overridden as 21

Final Property to prevent Overriding


When the user need not want others to access super class methods, properties or
subscripts swift introduces 'final' property to prevent overriding. Once 'final' property is
declared the subscripts won't allow the super class methods, properties and its subscripts
to be overridden. There is no provision to have 'final' property in 'super class'. When 'final'
property is declared the user is restricted to create further sub classes.


final class Circle {
final var radius = 12.5
var area: String {
return "of rectangle for \(radius) "
Free download pdf