Introduction of OOP
Tags: programming, swift, oop, ios
Published: 2023-01-14 15:04:58

###_Basics of OOP_

Object-oriented programming is a paradigm based on the concept of wrapping pieces of data, and behavior related to that data, into special bundles called objects, which are constructed from a set of “blueprints”, called classes. 

###_Objects, Classes_

> _Complex real-life requirements need to be modeled into instructions for the computer to understand._

Say you have a cat named Foo. Foo is an object, an instance of the Cat class. Every cat has a lot of standard attributes: name, age, weight, color, favorite food, etc. These are the class’s fields. 

All cats also behave similarly: they breathe, eat, run, and sleep. These are the class’s methods. Collectively, fields and methods can be referenced as the members of their class. 

Data stored inside the object’s fields are often referenced as state, and all the object’s methods define its behavior. 

###_Pillars of OOP_

Object-oriented programming is based on four pillars, concepts that differentiate it from other programming paradigms. 

* [Abstraction, Inheritance, Polymorphism, Encapsulation]

####_Abstraction_

> _Abstraction means to represent the essential feature without detailing the background implementation or internal working detail._

Abstraction lets you focus on what the object does instead of how it is done. The idea behind abstraction is knowing a thing on a high level. Abstraction helps in building independent modules which can interact with each other by some means. Independent modules also ease maintenance.

It is used for the following scenarios:
*Reduce complexity. (Create a simple interface)
*Allow for implementation to be modified without impacting its users.
*Create a common interface to support polymorphism (treating all implementations of the abstracted layer the same.

####_Inheritance_

> _Inheritance helps in organizing classes into a hierarchy and enabling these classes to inherit attributes and behavior from classes on top of existing ones in the hierarchy._

Example: A Parrot is a bird. US Dollar is a type of currency. Inheritance does not work backward. The parent won’t have properties of the derived class.

If you want to create a class that’s slightly different from an existing one, there’s no need to duplicate code. Instead, you extend the existing class and put the extra functionality into a resulting subclass, which inherits fields and methods of the superclass. if a superclass implements an interface, all of its subclasses must also implement it. 

####_Polymorphism_

> _Polymorphism is the ability of a program to detect the real class of an object and call its implementation even when its real type is unknown in the current context._

Polymorphism is the concept that there can be many different implementations of an executable unit and the difference happen all behind the scene without the caller's awareness.

Inheritance is one means of achieving polymorphism where behavior defined in the inherited class can be overridden by writing a custom implementation of the method. This is called method overriding also known as Run Time polymorphism. There is also one more form of polymorphism called method overloading where inheritance does not come into the picture. The method name is the same but the arguments in the method are different.


####_Encapsulation_

> _Encapsulation is the process of combining data and functions into a single unit called class. In Encapsulation, the data is not accessed directly; it is accessed through the functions present inside the class._

In simpler words, attributes of the class are kept private and public getter and setter methods are provided to manipulate these attributes. Thus, encapsulation makes the concept of data hiding possible.

To start a car engine, you only need to turn a key or press a button. You don’t need to connect wires under the hood, rotate the crankshaft, and initiate the power cycle of the engine. These details are hidden under the hood of the car.