Object Oriented Programming in Swift

Updated 28 February 2022

Save

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.

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.

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.

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.

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.

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

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

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.


author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home