Table of contents
1.
Introduction
1.1.
Syntax:
2.
Swift Overriding Methods
3.
Swift Overriding Properties
3.1.
Example:
4.
Prevent Method Overriding
5.
Frequently Asked Questions
5.1.
In Swift, how do you override a method?
5.2.
Is it possible to override properties in Swift?
5.3.
Is it feasible to have multiple inheritances in Swift?
6.
Key Takeaways
Last Updated: Mar 27, 2024

Swift Method Overriding

Author Mayank Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Overriding in Swift is when a subclass is responsible for changing or re-implementing the instance method, instance property, and type property defined in the parent class or superclass. Based on our requirements, a subclass provides a fully customized implementation for any type, including instance methods and properties of the superclass. In Swift, we utilize the override keyword to override inherited properties or methods in subclasses. This tells the compiler whether the overriding method or property description matches the base class or not.

The subclass inherits the superclass's methods and properties in Swift Inheritance. This lets subclasses have direct access to members of the superclass. When the same method is declared in both the superclass and the subclass, the subclass method takes precedence over the superclass method. Overriding is the term for this. To declare method overriding, we utilize the override keyword. As an example,

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

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

The Car subclass's displayInfo() method overrides the Vehicle superclass's displayInfo() method.

Syntax:

class Sample1 {
func Name() {
// Function implementation here
}
}
class Sample2: Sample1 {
override func Name() {
// Override function implementation here
}
}

In the above syntax, we defined base class "Sample1" and subclass "Sample2," inheriting base class properties and methods and overriding functions in the subclass using the override keyword.

Swift Overriding Methods

We can inherit base class properties and subclass methods and override base class properties or subclass methods based on our needs by using inheritance. We can override a base class method with the same class declaration by using the override keyword in a subclass. The following is an example of inheriting properties from a base class to a subclass and using the override keyword to override a base class method in a subclass.

class Class1 {
func FullName() {
print("It’s a Super Class")
}
}
class Class2: Class1 {
override func FullName() {
print("It’s a Sub Class")
}
}
let s1 = Class1()
s1.FullName()
let s2 = Class2()
s2.FullName()

As you can see in the example above, we created a base class "Class1" with the function "FullName," inherited base class properties from subclass "Class2," and overridden the base class function "FullName" in the subclass based on our needs.

Swift Overriding Properties

In Swift, we can override inherited properties of a base class in a subclass to provide new getters and setters for the properties or add property observers to the overriding properties to track when the property value changes based on our needs. We can inherit the base class properties by defining custom getters and setters for properties in subclasses.

Example:

class OldStudent {
var studentFee = 800
var Fee: String {
return "The Old Fee is \(studentFee)"
}
}
class NewStudent: OldStudent {
var amount = 1000
override var Fee: String {
return super.Fee + ", the override New Fee is \(amount)"
}
}
let newstudent = NewStudent()
newstudent.studentFee = 800
newstudent.amount = 1000
print("\(newstudent.Fee)")

In the example, we are overriding properties from the base class "OldStudent" in the subclass "NewStudent" to meet our needs. We get the following result when running the preceding example in the Swift playground.

Prevent Method Overriding

We can prevent the method from being overridden in Swift.

 

When declaring a method in the superclass, we use the final keyword to make it non-overridable. As an example,

 

class Vehicle {

  // prevent overriding
  final func displayInfo() {
    print("Four Wheeler or Two Wheeler")
  }
}

// Car inherits Vehicle
class Car: Vehicle {

  // attempt to override
  override func displayInfo() {
    print("Four Wheeler")
  }
}

// create an object of the subclass
var car1 = Car()

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

 

The displayInfo() method in the superclass has been tagged as final in the example above. We can't override a method that has been defined as final. As a result, we get an error when we try to override the final method.

Frequently Asked Questions

In Swift, how do you override a method?

The subclass inherits the superclass's methods and properties in Swift Inheritance. This lets subclasses have direct access to members of the superclass. When the same method is declared in both the superclass and the subclass, the subclass method takes precedence over the superclass method.

Is it possible to override properties in Swift?

In Swift, we can override inherited properties of a base class in a subclass to provide new getters and setters for the properties or add property observers to the overriding properties to track when the property value changes based on our needs.

Is it feasible to have multiple inheritances in Swift?

There is no support for multiple inheritances of classes in Swift since we can't declare a class with multiple base classes or superclasses. A subclass can inherit only one class. On the other hand, a class can follow one or more protocols.

Key Takeaways

In this article, we learn about swift overriding with some of its properties with the help of a few examples. I hope you all like this article.

I hope you all like this article. Want to learn more about Data Analysis? A few excellent courses can guide you in your learning process. You can also refer to our other courses like DSA and Machine Learning.

Happy Learning, Ninjas!

Live masterclass