Inheritance in Swift
A class can inherit methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass. A class that does not inherit from another class is known as a base class.
class someClass {
}
/** inheritting from someClass **/
class someSubclass: someClass {
//will have access to someClass properties
}
The above code snippet shows how the someSubclass
inherited from the someClass
class. Classes in Swift can call and access methods and properties belonging to their superclass and can provide their own overriding versions of those methods and properties to refine or modify their behavior. Classes can also add property observers to inherited properties in order to be notified when the value of a property changes.
Accessing Superclass Properties and Methods
Assuming we have a base class named Person
and a Youtuber
class that inherit from the Person class
class Person {
let name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
func celebrateBirthday() {
self.age += 1
print("Happy Birthday \(self.name)")
}
}
class Youtuber: Person {
let channelName: String
var subscribers: Int
//we need to pass in the name & age of the Youtuber Person
init(channelName: String, subscribers: Int, name: String, age: Int) {
self.channelName = channelName
self.subscribers = subscribers
//we use this to initialise the Person from within the subclass
super.init(name: String, age: Int)
}
}
The first thing I want us to notice is the class Youtuber: Person{}
. We simply said the Youtuber
be a type of Person
which make it inherit all the properties and method of the Person
class.
Secondly, when we initialize the Subclass
, we use the super.init()
method to pass the parameters needed by the superclass
constructor since a Youtuber
is actually still a Person
.
We can access the celebrateBirthday()
method of the Person
class from the Youtuber
subclass like below
Abel: Youtuber = Youtuber(channelName:"Swift",subscribers:32,name:"Abel",age:27)
Abel.celebrateBirthday() //Happy birthday Abel
Abel.age //27
Overriding Methods
At times, the subclass
might wish to override a method contained in a superclass
. We will make use of the override
keyword within the subclass
like below:
class Youtuber: Person {
let channelName: String
var subscribers: Int
//we need to pass in the name & age of the Youtuber Person
init(channelName: String, subscribers: Int, name: String, age: Int) {
self.channelName = channelName
self.subscribers = subscribers
//we use this to initialise the Person from within the subclass
super.init(name: String, age: Int)
}
/** This will override the celebrateBirthday in the superclass */
override func celebrateBirthday() {
self.age += 1
print("Hi, happy birthday from ur youtube subscribers")
}
}
Agoi: Person = Person(name: "Adeyemi", age: 27)
Agoi.celebrateBirthday() //Happy birthday Adeyemi
Abel: Youtuber = Youtuber(channelName:"Swift",subscribers:32,name:"Abel",age:27)
Abel.celebrateBirthday() //Hi, happy birthday from ur youtube subscribers
I initialized the Person
class as well as the Youtuber
class, the celebrateBirthday method in the Person class will return a different value to the Youtuber class because of the override
function that print a different content in the Youtuber class.
Overriding Properties
we have seen how to override methods
in swift, but there are cases where we actually need to override properties.
class Person {
var name: String
var gender: String = "Male"
init(name: String) {
self.name = name
}
}
class Foreigner: Person {
var country: String
init(name: String, country: String) {
super.init(name: name) //use to set the superclass initializer
//change the default gender property
self.gender = "Female"
self.country = country
}
}
var newForeigner: Foreigner = Foreigner(name: "Johndoe", country: "Canada")
newForeigner.gender //will return Female since we already changed it in the init method
Immediately after the super.init()
in the inside init
method, we set the country variable. Note that, we can only do this after we have initialize the superclass.
Preventing Override
We can prevent a method or property from been overridden by a subclass by using the keyword final func
or final var
.
There is more to inheritance like observers. Didn’t cover that here because am not pretty sure how to use it now. Will update this article when I fully understand the concept. Lemme go read up enum and optionals
. Yes, will write about it when done.
I am happy to share this article with you. If you’ve enjoyed this article, do show support by giving a few claps 👏_ . Thanks for your time and make sure to follow me or drop your comment below_ 👇