The full name of OOPs is Objected Oriented programming Language. OOPs is very important concept, on which almost all modern programming language stands.
SWIFT, APPLE’s trending language is no different from this. OOPs concepts are the backbone of SWIFT.
Let’s come to the topic which we will cover up in OOPs.
. Access Control
Let’s see each topic one by one:-
(1) classes are the blueprints of the objects.
(2) classes are combination of data members and functions.
(3) classes are not a real-world entity that’s why it does not occupy memory.
UIView, the iOS’s main UI class, can be considered a class.
(1) Object is instance of class.
(2) Object is a real-world entity that’s why it occupy memory.
(3) E.g, Dog, Cat, Pen, Pencil, etc, everything is an object. From our Person example, men and women are examples of objects which belong to the same class i.e Person.
When we create an outlet of UITableView, then we create an object of UITableView class.
Methods or Functions are the behaviour of the objects of a class. Let us say a person can walk, sing, play, etc irrespective of any object(Man/Woman). These all can be said to be methods/functions of the class.
UIView class has many methods. Eg. “setNeedsLayout()”, which we call if we want to forcefully reset our UI layout.
Swift provides quite many handy access control levels which are really helpful while writing our code. Some of the access levels provided SWIFT:
- Open access and public access — Entities with this access level can be accessed within the module that they are defined as well as outside their module.
- Internal access — Entities with this access level can be accessed by any files within the same module but not outside of it.
- File-private access — Entities with this access level can only be accessed within the defining source file.
- Private access — Entities with this access level can be accessed only within the defining enclosure.
Encapsulation is a concept by which we hide data and methods from outside intervention and usage.
What we did here:
- We declared a Maths class which does up some mathematical calculations.
- We declared two variables required for inputting values.
- Initialise the variables.
- We declare a method to add the two variables
- And then another method to display the result.
In the above example, we encapsulated the variable “result” by using the access specifier “private”. We hide the data of variable “result” from any outside intervention and usage.
Abstraction is hiding the details or internal implementation, it’s highlight a set of service that we are offering.
ATM Machine, Electric circuit.
Inheritance is defined as a process by which you inherit the properties of your parent. Technically, Inheritance is a process by which a child class inherits the properties of its parent class.
What we did here:
- We declared a child class Men which inherit the Person class…..Ya of course men are persons. Wait did you think of something else!! ;]
- And since Men class inherits from Person parent class, it can also access its properties as it does in the code.
UIButton class inherits from UIView thus is able to access its properties like backgroundColor, frame, etc.
Polymorphism is combination of two words first is “poly” means many and second is “morph” means form. So Polymorphism describe us, A thing or object have many form is called Polymorphism.
Example:- A Person.
Finally we can say Objected Oriented Programming is the powerful and backbone of any programming language. And very useful to solve very big problem. I hope you will like this.