Sobes.tech
Junior — Senior

What will be output in example №39

livecode

Task condition

This code snippet demonstrates how property overriding works when a property declared in a protocol has a default implementation in an extension, and a conforming class provides its own implementation. It compares the output of the property when accessed through the object's specific type and through the protocol type.

protocol TheProtocol {
    var value: String { get }
}

extension TheProtocol {
    var value: String {
        return "in TheProtocol"
    }
}

class TheClass: TheProtocol {
    var value: String {
        return "in TheClass"
    }
}

let theClass = TheClass()
let theProtocol: TheProtocol = theClass

print(theClass.value)      // ?
print(theProtocol.value)   // ?

Expected result: both calls print "in TheClass".