Swift Overriding Methods and Properties

In Swift Inheritance, the subclass inherits the methods and properties of the superclass. This allows subclasses to directly access the superclass members.

Now, if the same method is defined in both the superclass and the subclass, then the method of the subclass class overrides the method of the superclass. This is known as overriding.

We use the override keyword to declare method overriding. For example,

class Vehicle {
  
  func displayInfo(){
    ... 
  }
}

class Car: Vehicle {
  
  // override method
  override func displayInfo() {
    ... 
  }   
} 

Here, the displayInfo() method of the Car subclass overrides the same method of the Vehicle superclass.


Example: Swift Method Overriding

Output

Four Wheeler

In the above example, we are overriding the displayInfo() method of the superclass Vehicle inside the subclass Car.

// inside the Car class 
override func displayInfo() {
  print("Four Wheeler")
}

Here, we have used the override keyword to specify the overridden method.

Now, when we call the displayInfo() method using the object car1 of Car,

car1.displayInfo()

the method inside the subclass is called.

Swift Method Override
Working of method overriding

This is because the displayInfo() method of the Car subclass overrides the same method of the Vehicle superclass.


Access Overridden Method in Swift

To access the method of the superclass from the subclass, we use the super keyword. For example,

Output

Vehicle: Four Wheeler or Two Wheeler
Car: Four Wheeler

In the above example, the displayInfo() method of the Car subclass overrides the same method of the Vehicle superclass.

Inside the displayInfo() of Car, we have used

// call method of superclass
super.displayInfo()

to call the displayInfo() method of Vehicle

So, when we call the displayInfo() method using the car1 object

// call the displayInfo() method
car1.displayInfo()

both the overridden and the superclass version of the displayInfo() methods are executed.


Prevent Method Overriding

In Swift, we can prevent the method from overriding.

To make a method non-overridable, we use the final keyword while declaring a method in the superclass. For example,

In the above example, we have marked the displayInfo() method in the superclass as final.

Once the method is declared as final, we cannot override it. So when we try to override the final method,

override func displayInfo() {
  print("Four Wheeler")

we get an error message: error: instance method overrides a 'final' instance method.


Override Swift Properties

In Swift, we can override the computed properties. For example,

Output

New Fee: 10000

In the above example, we have created a computed property inside the superclass University. Notice the code inside the subclass Fee,

override var cost: Int {
  return 10000 
} 

We are overriding the computed property cost. Now, when we access the cost property using the object amount of Fee,

// access fee property
amount.cost

the property inside the subclass is called.

Note: We cannot override the stored properties in Swift. For example,

class A {
  // stored property
  var num = 0
}

class B: A {
  // overriding stored property 
  override var num = 2    // Error Code
}