Inheritance in Swift


In this tutorial, you are going to learn about the inheritance in Swift and some of the examples of Swift inheritance.

Inheritance is a common thing in OOP (Object Orient Programming) languages. Like any other OOP language, Swift also supports the concept of inheritance of class.

What is class inheritance in Swift?

In Swift, a class can inherit all the methods, properties and other characteristics of another class. Actually, inheritance is a fundamental behavior that allows a programmer to create a new class from an existing class.

Inheritance allows to have the same behavior as of another class and extend that behavior to provide specific requirements in programming.

Subclass and superclass

The new class that is created and inherits the methods and properties of the original class is known as the subclass or child class or derived class. On the other hand, the existing original class from which the subclass or child class is derived is known as the superclass or base class or parent class.

Syntax of Swift inheritance

In Swift programming, we use the colon (:) to inherit the class from another class (parent class or superclass).

Below is a simple example that shows how to inherit a class from the original class:

// define the superclass
class Person {
  // define properties and methods
}

// code for inheritance
class Employer: Person {

  // Defining properties and methods of Person
  // Defining properties and methods of Employer
}

In the above example, we have inherited the Employer class from the class Person. The Employer class can have all the properties and methods of the Person class. We can also define new methods and properties to the Person class.

Example of Swift inheritance

Below is the example of inheritance in Swift:

// define the superclass
class Person {
    // Define properties and method for superclass or parent class
    var fullName: String = ""
    func age() {
        print("Person age is 29")
    }
}

// code for inheritance
class Employer: Person {
    
    // Define method for subclass and access the fullName property from the superclass
    func salary() {
        print("57500 is the salary of",fullName)
    }
    
}

// Creating object of the subclass
var employerObj = Employer()

// Accessing property and method of the subclass
employerObj.fullName = "Linda Biden"
employerObj.age()

// Call the method from the subclass
employerObj.salary()

Output:

Person age is 29
57500 is the salary of Linda Biden

In our above example, we have created a subclass Employer which is inherited or derived from a superclass Person.

In the code we are accessing the property and method of the superclass from the object of the subclass which you can see below:

employerObj.fullName = "Linda Biden"
employerObj.age()

We are able to access the superclass property and method using the subclass object. It becomes possible because the subclass inherits all properties and methods of the superclass.

Method Overriding in Swift Inheritance

In our previous example, we have seen that it is possible to access the method of the superclass using the object of the subclass.

But did you ever think about what happened if we define the same method in the subclass that is already present in the superclass?

If we define the same method in the subclass that is already in the superclass, then the method of the subclass will override the method of the superclass. This phenomenon is known as the method overriding in Swift.

In Swift, we use the override keyword for method overriding.

Example of method overriding in Swift

class Person {
    // Define method in the superclass
    func age() {
        print("Person age is 29")
    }
}

// Employer class inherit the Person class
class Employer: Person {
    
    // Override the age() method
    override func age() {
        print("Employer age is 29")
    }
    
}

// Creating object of the subclass
var employerObj = Employer()

// Call the age() method
employerObj.age()

Output:

Employer age is 29

In the above example, we have defined the same method age() in the subclass. It already exists in the superclass. But when we call the method from the object of the subclass, it is calling the method of the Employer class.

This is happening because the age() method of the Employer overrides the age() method of the Person.

You can notice that we have used the override keyword to tell the compiler for the method to be overridden.

Access superclass method using super keyword

In the above example, we already have seen that the method in the subclass overrides the method in the superclass. But there is a way to access the class from the superclass.

We can access the method from superclass using the super keyword. See the example below:

class Person {
    // superclass method
    func age() {
        print("Person age is 29")
    }
}

// Employer class inherit the Person class
class Employer: Person {
    
    // Override the age() method
    override func age() {
        // Accessing method from superclass
        super.age()
        print("Employer age is 29")
    }
    
}

// Creating object of the subclass
var employerObj = Employer()

// Call the age() method
employerObj.age()

Output:

Person age is 29
Employer age is 29

In the above code, we are using the super keyword inside the age() method of subclass Employer to call the age() method from the superclass Person.

super.age()

In the output, we can see that both the age() method from the superclass and the age() method of the subclass are executed.

Prevent method override

You can prevent overriding a method by using the final modifier before the method. We have to use the final keyword in the superclass to prevent a developer from mistakenly overriding a method in the superclass.

Below is how we can do it:

class Person {
    // superclass method
        final func age() {
        print("Person age is 29")
    }
}

Now Swift will not allow overriding the age() method.

Advantages of using inheritance

By using inheritance, we can avoid writing duplicate code. It helps programmers and software developers to minimize writing the same code again.

Inheritance helps us to share the common code of a class with other developers. It helps a team to develop a project together.

Use inheritance when is-a relationship

In Swift programming, inheritance is an is-a relationship. That means we should use inheritance only in the time when there is an is-a relationship that exists between two classes.

For example,

  • Banana is a Fruit
  • Tiger is an Animal
  • Water is a Liquid

Here in the above example, Banana can be inherited from Fruit, Tiger can be inherited from Animal and Water can be inherited from Liquid.