​Swift is really a powerful and user-friendly coding language for macOS, iOS, watchOS and tvOS...

Page 145

Swift

} } class Rectangle: Circle { var print = 7 override var area: String { return super.area + " is now overridden as \(print)" } }

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: <stdin>:14:18: error: var overrides a 'final' var override var area: String { ^ <stdin>:7:9: note: overridden declaration is here var area: String { ^ <stdin>:12:11: error: inheritance from a final class 'Circle' class Rectangle: Circle { 133


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