Inheritance plays a rather large role in the Swift environment, so it makes sense to fully understand all the different methods of using inheritance to your benefit.
Credit: Some of the quotes used in this story derive from the Swift Documentation
A class can inherit methods, properties, and other characteristics from another class.
Basically this means that one class can take on the properties of another class.
Take the following code-snippet for an example:
In this file, we created two classes: an Animal and a Human class. As you saw, the Animal class just looks like a regular old class, while the Human class looks slightly peculiar.
With this being said, the Animal class (line 1) in this context is known as a “base class” because it does not inherit any properties from anywhere else. This means that it is a suitable underlying class for inheritance, if another class were to inherit from it.
The Human class is known as “subclass” because it will inherit the properties of the Animal superclass in this context. Further, with the definition of the two classes’ we now know that the Animal class is a superclass because it contains the properties to be inherited, while the Human class is a subclass because it has the inherited properties from the superclass.
When to use Super and Subclasses in Swift?
It’s a good idea to use superclasses in Swift, when you want to define the general way to do a certain thing, which then allows you to create a variety of subclasses that offer a more specialized way of doing certain things.
Take the following example:
In this example we have a total of 1 superclass and 3 subclasses. The Animal superclass defines the general aspects of an animal, while the Human Bird and Fish subclasses specialize the certain aspects of the animal.
Further Explanation of Subclasses
In the previous example the Human class is a subclass of the Animal class. With this being said, the Human class will inherit any accessible properties of the Animal class.
Therefore, to access the base class variable isEating, we can simply refer to the variable in a normal fashion even though the variable was never defined in the subclass, but rather in the base class. To do so, we can do the following:
As expected, this outputs:
Human is eating: true
Now, let’s say we wanted to access the function description() from the base class in the subclass. We can do so in the same manner as with variables via the following:
Again, as expected this would output the following:
Animal is eating: true
Overriding Previous Declarations!
Let’s say that we want to define a variable in the subclass, that has already been declared and inherited from the base class. How can we go about doing so?
Easy! We simply use the override keyword in Swift to override the inherited declaration.
In order to override the description() function, we will need to provide our own overriding function, which we can do via the following:
So in this example, we made a call to the description() function just like in the previous example, but this time it printed a specialized Human description instead of the general Animal description. It did this because we told Swift to override the inherited function declaration, and use this one instead.
Thanks for reading!
That’s all for this story! I hope you learned something new, but if you do happen to still be struggling with Subclassing in Swift, feel free to drop a response below!
Note: If you really really really enjoyed this post, please consider supporting me on Patreon! Peace!