Open classes can be subclassed within the module where they’re defined, and within any module that imports the module where they’re defined.

So far we have an open class named RegularPolygon and now we are going to define a new public class in the RegularPolygon framework, which will be a subclass of RegularPolygon class.

Let’s add a new .swift file under RegularPolygon framework and name it Square. Now define a public class named Square with a custom designated initializer, init(sideLength: UInt), which only takes one sideLength parameter since the square is always with 4 sides.

public class Square: RegularPolygon {

public init(sideLength: UInt) {

// Set numberOfSides equals to 4

// Pass down the sideLength parameter

super.init(n: 4, l: sideLength)

}

}

Open class members can be overridden by subclasses within the module where they’re defined, and within any module that imports the module where they’re defined.

Now we are going to override the open func description() -> String from Square’s superclass, RegularPolygon. the description() function in Square is marked with override and with public access (Overriding instance method has to be as accessible as its enclosing type, which in our case is open access or public access)

public class Square: RegularPolygon {

public init(sideLength: UInt) {...}

override public func description() -> String {

// Replacing original description to a specific name for 4-sided

// regular polygon: Square.

return "A square"

}

}

Everything looks great now👍. We can now go ahead and try to create an instance of this public Square class in ViewController.swift file’s viewDidLoad() function.

import UIKit

import RegularPolygon class ViewController: UIViewController {

override func viewDidLoad() {

...



let sq = Square(sideLength: 5)

let perimeter = sq.getPerimeter()

print("Perimeter of square = \(perimeter)")

// prints "Perimeter = 20"

print(sq.description())

// prints "A square"

}

}

Classes with public access, or any more restrictive access level, can be subclassed only within the module where they’re defined.

If we want to have a different kind of square, let’s say, a colored square. We can define a new class called ColorSquare. Since square class is with public access, we can only define a subclass of Square class within our RegularPolygon framework(module).

We can now create a new .swift file named ColorSquare under RegularPolygon framework and write down the definition of ColorSquare class.

We want this ColorSquare can also be used outside of its defining module, thus we define it as a public class with public initializer.

public class ColorSquare: Square {

var colorName: String

public init(colorName: String, sideLength: UInt) {

self.colorName = colorName

super.init(sideLength: sideLength)

} override public func description() -> String {

return "A \(colorName) square"

}

}

Class members with public access, or any more restrictive access level, can be overridden by subclasses only within the module where they’re defined.

Here we are going to override the description() function again in order to provide square’s color info in it.

public class ColorSquare: Square {

... override public func description() -> String {

return "A \(colorName) square"

}

}

Now the ColorSquare class is all set. To create a ColorSquare instance, we insert new lines of codes as shown below.

import UIKit

import RegularPolygon class ViewController: UIViewController {

override func viewDidLoad() {

...



let csq = ColorSquare(colorName: "blue", sideLength: 5)

let perimeter = csq.getPerimeter()

print("Perimeter of square = \(perimeter)")

// prints "Perimeter = 20"

print(sq.description())

// prints "A blue square"

}

}

Congrats! we finish all the examples. 🎉🎉🎉