Swift inheritance: The definition, when and how to use it effectively

If you are an iOS app developer, you should consider a powerful programming language called Swift. Swift is a programming language of Apple. It was released in 2014. In order to continue the previous article about Swift tuple, in this blog today, ArrowHiTech will introduce the Swift inheritance: The definition, when, and how to use it effectively. So let’s start. 

What’s swift inheritance?

A class can inherit attributes, methods, and other features from another class in the Swift programming language. Swift inheritance is the process of passing certain properties and attributes from one class to another. Swift classes can inherit things from a superclass. It’s like you can inherit genes from your mother and even grandparents.

Subclass: A child class or subclass is a class that inherits properties from another class.

Super class: The parent class or super class is the main class. It comes from the properties of the subclass that are inherited.

Superclasses in Swift4 classes are used to call and access methods, properties, functions, and override methods. To add a property and modify the stored and computed property methods, property observers are utilized.


class class_name: Superclass_name   


// Definition of the class  


Overriding Functions and Properties

One of the most common reasons for using inheritance is to reuse some of a class’s current code and replacing other sections. Instead than duplicating existing code, you reuse parts of it.

Overriding Functions in Swift inheritance

To override a function, you must do two things:

  • Rewrite the function signature exactly. It includes the parameters and return type.
  • Add the override keyword to the function signature.

Why does the function declaration need to be repeated? You can’t modify the function name, parameters, or return type while overriding a function. They should be identical to the function you’re overriding.

For example: 

Swift inheritance

If we apply the same code we used before, we get the following result:

Swift inheritance

Because we’ve overridden the color property of Bike, the new version of the describe() function now uses it. What if you need to access the superclass’s “previous” implementation of the function you’re overriding? You can use the super keyword to go to that function.

Use of super keyword

The keyword ‘super‘ is used as a prefix to access the super class’s methods, properties, and subscripts.

OverridingAccess to methods,properties and subscripts

Why You Can’t Override Properties in Swift inheritance

Is it possible to override properties in Swift inheritance as well? Yes and no. So take a look at this:

iOS programming ;language

We’re trying to override the speed property in the Bike subclass by changing its type from Int to Double in the code above. But this isn’t going to work! So what’s to prevent you? 

Firstly, overriding allows you to supply a custom implementation. Therefore, you can customize the function or property’s “contents,”. However, you can’t change its definition. In addition, you can’t modify the types and/or parameters of a function or a property.

Secondly, remember how we said a Unicycle is a Bike, and a Bike is a Vehicle? As a result, when a type of Vehicle is requested, we can offer an instance of type Unicycle. Because the two are linked. Take a look at this:

iOS app development

This function determines how long a vehicle would take to drive a certain distance. Have you noticed the parameters of the function? One is of the Vehicle kind. And the other is for distance. 

Therefore, the key to overriding is that you can’t modify the types in the code that depend on the property or function to work..

If you modify an input or output type that is used by other programs, you will almost surely introduce errors into the codebase. As a result, you can’t modify attributes or modify the parameters or return type of a function by design.

When to use Swift inheritance?

The main aim for using Swift inheritance is to reuse code. Assume you’re working on an app that involves vehicles, such as cars, bikes, unicycles, and motorbikes. It makes sense to build a hierarchy because those things definitely have a link.

  • Vehicle
    • Car
      • RaceCar
      • FamilyCar
    • Bike
      • Bicycle
      • Motorcycle
      • Unicycle

It’s reasonable to assume that a RaceCar and a FamilyCar share features gained from Car. Because they’re both vehicles, a Unicycle and a RaceCar have some same features.

However, instead of automobiles and unicycles, your next app is likely to feature buttons, text fields, or table view cells. So when do you use inheritance and subclassing!?

The view controller is a popular use case. In fact, you’re subclassing the current UIViewController class every time you put a view controller on the screen! That is the only way it will work.

You inherit the functionality of a view controller when you use view controllers. You also give your own viewDidLoad() implementation, as well as additional properties, functions, and so on.

The UIKit framework understands how to display a view controller and, when appropriate, calls your custom viewDidLoad() function, which displays your User Interface.

Alternatives to Subclassing

In Swift inheritance, subclassing has three major drawbacks:

  • You also inherit unneeded functions and properties.
  • It builds a tight class hierarchy.
  • When types are related but not descendants, it’s useless.

SwiftUI using three subclassing alternatives:

  • Protocols: A View is a protocol in SwiftUI. As a result, it has features that you must offer, such as body. The SwiftUI framework then depends on the fact that all views that follow the View protocol include a body property, which it uses to display the content.
  • Extensions: An extension, unlike a subclass, will add new functions to an existing type. This allows you to extend and reuse existing types without having to subclass them. Existing types can be made to follow a protocol using a protocol extension, which aids in the creation of composable programs.
  • Generics: A generic is a type or function that is adaptable. A generic function, for example, can multiply integers and doubles without having to declare the same function twice. This enables you to share/reuse functionality across many kinds without having to subclass or repeat code. Furthermore, opaque types make a concrete type dependent on its implementation, allowing for even more code flexibility.

Further Reading

Thanks to the Swift inheritance, you can save much time for writing Swift code for your iOS app development. If you are looking for useful and clear instruction about Swift, you should consider the following articles: