Sobes.tech
Junior — Middle

Is it possible to use different parameter names in method overriding?

sobes.tech AI

Answer from AI

In Swift, when overriding methods, you can use different parameter names in the signature, but it is important to understand that the compiler cares about external parameter names, not internal parameter names.

When overriding a method, the external interface must match so that the method is correctly overridden. Internal parameter names can differ, as they are only visible within the method body.

Example:

class Parent {
    func greet(name personName: String) {
        print("Hello, \(personName)")
    }
}

class Child: Parent {
    override func greet(name friendName: String) { // external parameter "name" matches
        print("Hi, \(friendName)")
    }
}

Here, the external parameter name matches, but the internal names (personName and friendName) differ — this is permissible.

Is it possible to use different parameter names in… - sobes.tech