Introduction:
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.
. Classes
. Objects
. Methods
. Access Control
. Encapsulation
. Abstraction
. Inheritance
. Polymorphism
Let’s see each topic one by one:-
Classes:
(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.
Syntax:
iOS Example:
UIView, the iOS’s main UI class, can be considered a class.
Objects:
(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.
Syntax:
iOS Example:
When we create an outlet of UITableView, then we create an object of UITableView class.
Methods:
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.
Coding Syntax:
iOS Example:
UIView class has many methods. Eg. “setNeedsLayout()”, which we call if we want to forcefully reset our UI layout.
Access Control:
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:
Encapsulation is a concept by which we hide data and methods from outside intervention and usage.
Coding Example:
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:
Abstraction is hiding the details or internal implementation, it’s highlight a set of service that we are offering.
E.g:-
ATM Machine, Electric circuit.
Inheritance:
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.
Coding Example:
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.
iOS Example:
UIButton class inherits from UIView thus is able to access its properties like backgroundColor, frame, etc.
Polymorphism:
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.
Conclusion
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.