O'Reilly logo

Object–Oriented Programming with Swift 2 by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Declaring classes that inherit from another class

The following lines show the code for the Animal base class in Swift. The class header doesn't specify a base class, so this class will become our base class for the other classes:

public class Animal { public static var numberOfLegs: Int { get { return 0; } } public static var averageNumberOfChildren: Int { get { return 0; } } public static var abilityToFly: Bool { get { return false; } } public var age: Int init(age : Int) { self.age = age print("Animal created") } public static func printALeg() { preconditionFailure("The pringALeg method must be overriden") } public func printLegs() { for _ in 0..<self.dynamicType.numberOfLegs { self.dynamicType.printALeg() } print(String()) } public static func ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required