In this blog, we will see Object-Oriented programming in swift. It is a programming paradigm that is based on the concept of objects. Let’s start with classes.
Classes
Class is a blueprint from which an object is created. To declare a class we use the class keyword.
1 2 3 4 5 6 7 8 |
class Birds { let legs:Int let feathers:String init(legs:Int,feathers:String) { self.legs = legs self.feathers = feathers } } |
In the above example, we have created a class Bird, which has properties legs, feathers. All the birds have different characteristics, but they share some common properties such as legs, feathers.
An example of class In swift is UIView.
Objects
It is the implementation of the class which can access any methods and properties of a class. We can create multiple objects of the same class but class declared only once.
1 |
let parrot = Birds(legs: 2, feathers: "green") |
An example of objects in swift is that when we create the outlet of UICollectionView then we create an object of it.
Properties
In the above example legs, the feather is the property of Birds. In swift point of view isEnabled, textColor is the properties.
Methods
Methods are the behavior. In the below example canFly is a method that indicates the behavior of the birds.
1 2 3 |
func canFly(){ print("can fly") } |
Encapsulations
It is the binding of the data member and member methods in a single unit. We can achieve encapsulation by Access control.
Abstraction
Abstraction is hidding the implementation and only showing the relevant data and method of the object. In the above example if someone wants to see the parrot fly then he is simply interested in seeing the parrot fly but not interested in how it is actually flying. Therefore, in the above scenario, only canFly() method needs to call.
Polymorphism
It means more than one form of the same type. In the above example, we created a method canFly(), which means birds can fly but there is some exception for birds that can’t fly.
1 2 3 4 5 6 7 8 |
class Kiwi:Birds{ override func canFly() { print("can't fly") } } let kiwi = Kiwi(legs: 2, feathers: "black") kiwi.canFly() |
The example which we mentioned above has object of Kiwi, and as it is a bird but it can’t fly so we have use the override keyword to override the method canFly and thus we achieve Method Overriding.
In swift when creating any ViewController and since it is a subclass of UIViewController so we override its method viewDidLoad(). In the Kiwi example if we declare two methods with the same name but different parameters then it will be Method Overlodaing.
1 2 3 4 5 6 7 8 |
class kiwi:Birds{ func eat(){} func eat(food:String,quantity:Int){} override func canFly() { print("can't fly") } } |
Inheritance
Ihneritance is the process of accuring the properties of the parent class into the child class. As we can see kiwi is inheritated from the bird. So it can access the properties of birds
1 2 |
let kiwi = Kiwi(legs: 2, feathers: "black") kiwi.feather = "gray" |
In swift as the label is child class of UIView, so it can access its properties isHidden.
Access Control
Swift provide four level of Access control
- private: It is visible only within the class.
- fileprivate: It is visible in the same file from anywhere.
- internal: It is visibile from anywhere within the module.
- public: It is visible from anywhere outside the module.
- open: It has highest access level and it can be subclassed or overridden from outside.
Thanks for reading this blog.
You can also check other blogs from here. If you have any issues, queries, or suggestions then you can leave your issues/query/suggestion in the comment section.