More about classes in Swift
Classes are like building complex data types, which means it holds multiple data types. Classes look similar to a struct, but there are a couple of points that differ.
There is no automatic memberwise initializer, you need to declare on your own. In classes, if you make a copy of the instance and you try to change the value, then the value of the original object will also get changed.
The instance of the class is called an object.
Initializing an Object
The variables declared inside the classes are called properties and Functions inside a class are called methods. If we initialize our class like a struct, Swift won’t allow us to compile our code. In struct, it automatically generates memberwise initializers but in classes, you need to create the initializer on your own.
To do this create a method inside the class and name it as init().
You need to note that before init() method don’t write func because this init() is special. We are passing the same property name in the init() method so you need to use self. to make more meaningful that we are assigning those value to the property which are inside the classes.
Creating instances of that class looks just the similar as struct:
A class based on an existing class inheriting all its properties and methods and adding a code on top of the inherited methods is called Class Inheritance. The class you inherit from is called parent or superclass and the new class is called a child class. The colon between the class means Employee class extends Person class.
Here we are creating a new class Employee and we are inheriting Person’s all properties and methods.
We can also give Employee our own initializer like this
Swift always allows you to super.init() because some important work has to be done when the parent class is initialized.
I have created an aboutMe() function in parent class i.e in Person class:
In Swift if you want to use the same method in the child class that exists in the parent class then you should know about the keyword: override. This will help you to execute the parent class function as well as the child class function if you use super.
If you use super.aboutMe() in child class then it will execute the parent class function and then it will execute the child class function.
Here you can see the parent class function is executed first and then the child class function is executed.
Here super.aboutMe() is written at the end of the function that's why the child class function is executed first and then the parent class.
Here I have removed the super.aboutMe() function that's why the parent class function is not executed here.
Sometimes you want to disallow developers from building their own class based on yours so in that case, you can use the final keyword before your class so that no other developers can change the behavior of your class.
In our case, I have added the final keywoard before Person. It throws an error in the Employee class because we cannot inherit the Person class.
In class, if you copy an object both the original and duplicate objects point to same value means if you change the value of original object duplicated object value too changes.
So now I’m trying to change the value of the original Employee value lets see what happens
Here you can clearly see I changed the original Employee name i.e emp1 from “Joy” to “Agnel Selvan”. The value gets changed in emp1Copy too this is because both the objects point to the same location in memory.
Deinitializers in Classes
You can run the code when the instance of the class is destroyed this is called Deinitializers. You need to write the code when the instance of the class is destroyed inside deinit.
Let's demonstrate with an example by creating an Employee loop so that the new Employee instance will be created and then destroyed.
If you have a constant class with variable property, that property can be changed. Because of this classes doesn’t need mutating keyword.
If you want to avoid changing the variable property value then you need to make that variable constant by adding let keyword. Like this
A more detailed Blog on Polymorphism and Typecasting will see in the next Blog. If you want to know more about Struct do refer to this
I hope the above article will help to clear the concepts of Classes. If the article has helped you and you want me to continue writing similar articles, you are welcome to support me.