Swift is a strong and intuitive development language for macOS, iOS, watchOS and tvOS....

Page 144

Swift

} }

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 Radius of rectangle for 100.0

is now overridden as 3 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) " 132


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.