Swift Overriding Methods and Properties

In Swift, we can override methods and properties of a superclass in a subclass. In this article, we will learn about how to override methods and properties, access overridden methods, and prevent method overriding. We will also look at how to override and access properties in Swift.

Override Methods in Swift

Overriding methods means providing a new implementation of an existing method in a superclass. The overridden method in the subclass should have the same name, parameters, and return type as the method in the superclass.

Here's the syntax for overriding a method in Swift:

1class Superclass { 2 func someMethod() { 3 print("This is a method of superclass") 4 } 5} 6 7class Subclass: Superclass { 8 override func someMethod() { 9 print("This is a method of subclass") 10 } 11}

In the above example, we have created two classes: Superclass and Subclass. The Subclass inherits the someMethod() method from the Superclass and overrides it with its own implementation.

We can also access the overridden method from the subclass using the super keyword.

1class Subclass: Superclass { 2 override func someMethod() { 3 super.someMethod() 4 print("This is a method of subclass") 5 } 6}

In the above example, we first call the someMethod() method of the superclass using the super keyword and then print a message.

Prevent Method Overriding in Swift

In Swift, we can prevent a method from being overridden in a subclass by marking it as final.

1class Superclass { 2 final func someMethod() { 3 print("This is a final method of superclass") 4 } 5}

In the above example, the someMethod() method of the Superclass is marked as final. This means that the someMethod() method cannot be overridden in any subclass.

Override Properties in Swift

In Swift, we can also override properties of a superclass in a subclass. When we override a property, we provide a new implementation of its getter or setter or both.

Here's the syntax for overriding a property in Swift:

1class Superclass { 2 var someProperty: String { 3 return "This is a property of superclass" 4 } 5} 6 7class Subclass: Superclass { 8 override var someProperty: String { 9 return "This is a property of subclass" 10 } 11}

In the above example, we have overridden the someProperty property of the Superclass in the Subclass.

We can also override only the getter or setter of a property.

1class Subclass: Superclass { 2 override var someProperty: String { 3 get { 4 return "This is a property of subclass" 5 } 6 } 7}

In the above example, we have only overridden the getter of the someProperty property of the Superclass.

Access Overridden Properties in Swift

We can also access the overridden properties of a superclass in a subclass using the super keyword.

1class Subclass: Superclass { 2 override var someProperty: String { 3 get { 4 return super.someProperty + " and this is an overridden property" 5 } 6 } 7}

In the above example, we have accessed the someProperty property of the Superclass using the super keyword and then returned a concatenated string.