What is the inheritance in Swift ?

Alright. Let’s talk about inheritance in swift. Inheritance has the concept of object-oriented development. Inheritance allows  classes to have some set of attributes or properties and then other classes can be derived from that class. The derived class can inherit all the features from the class that it inherited from. 

Inheritance performs a hierarchy between classes. The top class is called the base class and the derived classes known as subclass. But some people call the base class as parent and subclass as child classes. We can create a child class that inherits from another child class. So you can perform a tree-like hierarchy with the classes. The parent class is also known as superclass. In Swift a class can only have one parent class. This is called Single Inheritance. Inheritance is the most important feature of classes that it seperates class from structures.

Child classes can access the properties, functions, subscripts of their parent class. They can also override the properties, functions and subscripts of their parent class.So let’s make an example and see how it works. Create a swift file called vehicles.swift as shown below :

import Foundation
 
class Vehicle {
    var vehicleType = ""
    var fuelType = ""
    var wheelNumbers = 0
    func runVehicle()->String {
        return "Vehicle is running"
    }
}

Okay. I am going to create another file to inherit from the Vehicle class.  Let’s call it  cars.swift . The content will be like this :

import Foundation
 
class Car : Vehicle {
    var maxSpeed = 0
    var brandName = ""
    init(maxSpeed: Int, fuelType: String, wheelNumbers: Int, brandName : String) {
        super.init()
        super.fuelType = fuelType
        super.wheelNumbers = wheelNumbers
        self.maxSpeed = maxSpeed
        self.brandName = brandName
        
    }
}

As you can see we used : (colon) notation to represent inheritance. In this case the Car class can access all the Vehicle class features. In the initializer function of the Car class, we changed the fuelType and wheelNumbers properties value according to the initializer’s method parameters. But before doing that we must initialize the parent class with the super.init() command. Now we will call the Car class within the main.swift file :

import Foundation
 
var myCar = Car(maxSpeed: 300, fuelType: "diesel", wheelNumbers: 4, brandName : "mercedes")
 
print(myCar.brandName)
print(myCar.fuelType)

If we run the main.swift file then we can see the brandName and fuelType values that were just assigned in the output. Please pay attention to the fuelType value. Because we accessed this value by myCar object. As we said in the above we can override the parent class’s function within the child class. To do that let’s change the content of cars.swift file a little like below :

import Foundation
 
class Car : Vehicle {
    var maxSpeed = 0
    var brandName = ""
    init(maxSpeed: Int, fuelType: String, wheelNumbers: Int, brandName : String) {  
        super.init()
        super.fuelType = fuelType
        super.wheelNumbers = wheelNumbers
        self.maxSpeed = maxSpeed
        self.brandName = brandName
 
    }
    override func runVehicle()->String {
        return "Car is running"
    }  
}

If we try to run the RunVehicle function within the main.swift file as adding this line of code :

print(myCar.runVehicle())

Then we can see that the output value will be “Car is running” . That is because we are overriding this function. If you want to prevent  a property or a function of parent class to be overridden you can use the final keyword. Just adding the final keyword before the definition of a function or property is enough. For example if we want to prevent overriding for RunVehicle function, we can change the definition of the parent class like this :

final func runVehicle()->String {
        return "Vehicle is running"
}

After this definition, any other child class cannot override the runVehicle function.

That’s it for this post guys. I think you got the idea of inheritance in swift. See you soon

You may also like...