Menu
Coddy logo textTech

Overriding Methods

Part of the Object Oriented Programming section of Coddy's Swift journey. Lesson 23 of 57.

A subclass can replace an inherited method with its own version. The keyword override is required, which prevents replacing a method by accident:

class Animal {
    func sound() -> String { "..." }
}

class Dog: Animal {
    override func sound() -> String { "Woof" }
}

class Cat: Animal {
    override func sound() -> String { "Meow" }
}

print(Dog().sound())
print(Cat().sound())
print(Animal().sound())

Output:

Woof
Meow
...

super.method() calls the superclass version from inside the override, so the subclass can add to the inherited behaviour instead of replacing all of it:

class Report {
    func text() -> String { "Sales: 120" }
}

class SignedReport: Report {
    override func text() -> String {
        super.text() + " (signed by Ada)"
    }
}

print(SignedReport().text())

Output:

Sales: 120 (signed by Ada)

Computed properties can be overridden too, with override var:

class Shape {
    var sides: Int { 0 }
    var summary: String { "\(sides) sides" }
}

class Triangle: Shape {
    override var sides: Int { 3 }
}

print(Triangle().summary)

Output:

3 sides

The override runs even when the method is called from inherited code: summary above is written in Shape, yet it uses Triangle's sides. Swift always calls the version of the actual object, which is what makes overriding useful:

class Greeter {
    func name() -> String { "someone" }
    func greet() -> String { "Hello, \(name())" }
}

class AdaGreeter: Greeter {
    override func name() -> String { "Ada" }
}

let g: Greeter = AdaGreeter()
print(g.greet())

Output:

Hello, Ada
challenge icon

Challenge

Easy

Notification has a method render() that returns [info] text. Declare the subclasses Warning, whose render() returns [warn] TEXT (the text in capital letters), and Reminder, which adds a stored minutes and whose render() returns the superclass result followed by (in 10 min), using super.

The supplied code reads lines info,text, warn,text or remind,text,10, builds the right object in an array of Notification and prints render() for each.

Your code goes in Notification.swift, Warning.swift and Reminder.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

// Supplied input/output code: keep it as it is
var input: [String] = []
while let line = readLine() { input.append(line) }
var all: [Notification] = []
for line in input {
    let p = line.split(separator: ",").map(String.init)
    switch p[0] {
    case "warn": all.append(Warning(text: p[1]))
    case "remind":
        let r = Reminder(text: p[1])
        r.minutes = Int(p[2])!
        all.append(r)
    default: all.append(Notification(text: p[1]))
    }
}
for n in all { print(n.render()) }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming

Practice on your own: Swift playground