Swift Method Overriding

Method overriding is a fundamental feature of object-oriented programming that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. In Swift, method overriding enables developers to modify or extend the behaviour of inherited methods, making classes more flexible and adaptable.


What is Method Overriding?

Method overriding occurs when a subclass provides its own implementation of a method that is defined in its superclass. The overriding method must have the same name, parameters, and return type as the method in the superclass. The override keyword is used to indicate that a method is being overridden.

Syntax:

</>
Copy
class Superclass {
  func methodName() {
    // Original implementation
  }
}

class Subclass: Superclass {
  override func methodName() {
    // Overridden implementation
  }
}

Declaring a Superclass and Overriding Methods

Let’s define a superclass Student with a method displayDetails, and then override this method in a subclass GraduateStudent.

Example: main.swift

</>
Copy
class Student {
  var name: String
  var age: Int
  
  init(name: String, age: Int) {
    self.name = name
    self.age = age
  }
  
  func displayDetails() {
    print("Name: \(name), Age: \(age)")
  }
}

class GraduateStudent: Student {
  var degree: String
  
  init(name: String, age: Int, degree: String) {
    self.degree = degree
    super.init(name: name, age: age)
  }
  
  override func displayDetails() {
    print("Name: \(name), Age: \(age), Degree: \(degree)")
  }
}

let gradStudent = GraduateStudent(name: "Arjun", age: 24, degree: "M.Sc")
gradStudent.displayDetails()

Explanation:

  • The Student class defines the displayDetails method to print the student’s name and age.
  • The GraduateStudent subclass overrides the displayDetails method to include the degree information.
  • The super keyword is used to call the superclass initializer.

Output:

Output to Example for Declaring a Superclass and Overriding Methods in Swift

Accessing Superclass Methods

Even when a method is overridden, you can still access the superclass implementation using the super keyword.

Example: main.swift

</>
Copy
class Student {
  var name: String
  var age: Int
  
  init(name: String, age: Int) {
    self.name = name
    self.age = age
  }
  
  func displayDetails() {
    print("Name: \(name), Age: \(age)")
  }
}

class GraduateStudent: Student {
  var degree: String
  
  init(name: String, age: Int, degree: String) {
    self.degree = degree
    super.init(name: name, age: age)
  }
  
  override func displayDetails() {
    super.displayDetails()
    print("Degree: \(degree)")
  }
}

let gradStudent = GraduateStudent(name: "Ram", age: 26, degree: "MBA")
gradStudent.displayDetails()

Explanation:

  • The super.displayDetails() call invokes the displayDetails method of the Student superclass.
  • Additional functionality is added in the overridden method to print the degree.

Output:

Output to Example for Accessing Superclass Methods

Rules for Method Overriding

  1. The method in the subclass must use the override keyword.
  2. The overriding method must have the same name, parameters, and return type as the method in the superclass.
  3. The superclass method being overridden must be accessible (e.g., not marked as private).

Why Use Method Overriding?

Method overriding is useful for:

  1. Customizing behavior in subclasses.
  2. Adding additional functionality while preserving the base implementation.
  3. Creating polymorphic behavior where objects of different subclasses can be treated uniformly but exhibit different behavior.