Explain some common features of Protocol & Superclass in Swift


In Swift, there are two different concepts i.e. protocols and superclasses are used to build the application by writing reusable and flexible code. They both are different concepts but somehow they have some common features.

You can define the protocol with methods and properties to achieve any particular feature. In the same way, you can create a class with methods and properties to hold some information. Swift provides flexibility in that class, structure, and enums can conform to a protocol.

A superclass is also a class type. It can be inherited from other classes in a similar way. In this case, the superclass can be called a parent class and the inherited class might be called a subclass. This process is called inheritance.

In Swift, all the predefined classes such as UIView, UIButton, UIViewController, etc have a single superclass. This superclass is called NSObject which is the root class in Apple Ecosystem. The subclasses inherited by any superclass can take advantage of the properties and methods by addition own features.

Some common features of protocols and superclasses in Swift include

  • You can use protocols to define an interface to make a combination of related types. You can define optional methods too in the protocol. While superclasses can be used to define a common blueprint with properties and methods.

  • The protocols can be conformed by classes, structures, and enums. While superclasses can be used to create subclasses by class type only.

  • Protocols can be used as a type, which allows you to use polymorphism.

  • You can define the optional methods in a protocol. Basically, the methods which are not required to implement are always made them optional.

Protocols in Swift are similar to interfaces in other languages, in that they define a set of methods, properties, and other requirements that a conforming type must implement.

In the below example, you will define a protocol named Movable with a method. If any other type conforms to this protocol, it is required to implement this method. We will create a class and conform to the Movable protocol. Let’s see an example to create and conform to the protocol.

Example

import Foundation
protocol Movable {
   func move()
}
class Car: Movable {
   func move() {
      // implementation of the move method for Car
   }
}
struct Person: Movable {
   func move() {
      // implementation of the move method for Person
   }
}

In the above example, you have defined a protocol named “Movable”. Now, you are conforming to this protocol in two different types i.e. Car and Person. You can see the protocol can be conformed by class and structure both. You have defined a required method move() in the protocol, which needs to be implemented in Car and Person types.

In the other case, a class is inherited by a superclass. In Swift, the NSObject class is the root class and only the single class from which all classes inherit. For example, a superclass Vehicle is created with some properties and methods such as numberOfWheels and startEngine() that might be common for subclasses. For adopting these properties and methods, you can create subclasses like Car, Bike, Truck, etc. You can add your own properties and methods in the subclasses including the properties of the Vehicle class.

Example

class Vehicle {
   var numberOfWheels: Int
   func startEngine() {
      // implementation of startEngine method
   }
}
class Car: Vehicle {
   // Car has access to numberOfWheels and startEngine from the superclass
   // can also add its own properties and methods
}
class Bike: Vehicle {
   // Bike has access to numberOfWheels and startEngine from the superclass
   // can also add its own properties and methods
}

You should note down here that protocols and superclasses can be used together. For example, you can define a protocol that a class conforms to, and that class can also inherit from a superclass.

Example

protocol Movable {
   func move()
}
class Vehicle: Movable {
   func move() {
      // implementation of move method
   }
}
class Car: Vehicle {
   // Car has access to the move method from the superclass
   // can also add its own properties and methods
}

In this example, we created a class called Vehicle that is conforming the Movable protocol. The Movable protocol has a method called move() that is required to be implemented in the Vehicle class.

In the further step, we created a subclass called Car inherited by the Vehicle class. As a result, it can be used both when a Movable is needed and when a Vehicle is required.

Conclusion

Protocols and superclasses are both essential features in Swift programming that play different roles and applications. Protocols can define the methods, properties, and other requirements that a conforming type must implement that allow them to be used interchangeably.

In contrast, superclasses are used to define a base implementation of methods and properties that subclasses can inherit and build on. Protocols and superclasses can also be used in tandem, with a class conforming to a protocol while also inheriting from a superclass.

Understanding how to use protocols and superclasses correctly can help you organize, reuse, and maintain your code.

Updated on: 28-Feb-2023

178 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements