OOPs with Swift and real life example

What is OOPs?

OOP is Nothing but Object Oriented Programming.

Object-oriented programming (OOP) is a programming paradigm that uses “objects” and their interactions to design applications and computer programs.

OOPs have following features 

1. Object   – Instance of class
2. Class    – Blueprint of Object
3. encapsulation  – Protecting our data
4. polymorphism   – Different behaviors at diff. instances
5. abstraction    – Hiding our irrelevance data
6. inheritance    – one property of an object is acquiring to another property of an object

1. Object
Basically, an object is anything that is identifiable as a single material item. You can see around and find many objects like Mobile, Laptop etc. In OOP perspective, an object is nothing but an instance of a class that contains real values instead of variables.

Practical Ex.   var sandesh = human()

2. Class

A class is a template definition of the methods and variables for a particular kind of object. In other words, a class is a blueprint from which individual objects are created.

A Mobile can be a class which has some attributes like Profile Type, IMEI Number, Processor, and some more.) & operations like Dial, Receive & SendMessage.

Practical Ex. 

Class human {
properties – eye, mouth, nose, legs, hands
methods – talk(), walk()

3. Data Abstraction / Data hiding

Abstraction says, only show relevant details and rest all hide it

Some features of mobiles

  1. Dialing a number call some method internally which concatenate the numbers and displays it on screen but what is it doing we don’t know.
  2. Clicking on green button actual send signals to calling person’s mobile but we are unaware of how it is doing.

This is called abstraction where creating method which is taking some parameter & returning some result after some logic execution without understating what is written within the method

for ex. by using access specifier we can hide some data from other class/struct.

4. Encapsulation
Encapsulation is defined as the process of enclosing one or more details from outside world through access right. It says how much access should be given to particular details. Both Abstraction & Encapsulation works hand in hand because Abstraction says what details to be made visible & Encapsulation provides the level of access right to that visible details. i.e. – It implements the desired level of abstraction.

Talking about Bluetooth which we usually have it in our mobile. When we switch on the Bluetooth I am able to connect another mobile but not able to access the other mobile features like dialing a number, accessing inbox etc. This is because, Bluetooth feature is given some level of abstraction.

Another point is when mobile A is connected with mobile B via Bluetooth whereas mobile B is already connected to mobile C then A is not allowed to connect C via B. This is because of accessibility restriction.
This is handled by access specifier like public, private, protected, and internal

Practical Ex. – Class is best example of Encapsulation. Because class has encapsulated multiple things like property, methods and it present it as a one entity. Multiple things encapsulated in single thing is Encapsulation.

5. Inheritance

Inheritance is the ability to define a new class or object that inherits the behaviour and its functionality of an existing class. The new class or object is called a child or subclass or derived class while the original class is called parent or base class.

Considering the example,  Basic Mobile functionality is to Send Message, dial & receive call. So the brands of mobile is using this basic functionality by extending the mobile class functionality and adding their own new features to their respective brand.


6. Polymorphism

Runtime – Method Overloading
Compile time –  Method overriding

Runtime – Method Overloading
In Same class two methods with same name but diff parameter arguments.

Compile time –  Method overriding
In diff class two methods with same name and same parameter.
Polymorphism applies to overriding, not to overloading

Polymorphism can be defined as the ability of doing the same operation but with different type of input.

More precisely we say it as ‘many forms of single entity’. This play a vital role in the concept of OOPS.

Let’s say Samsung mobile have the 5MP camera available i.e. – it is having a functionality of CameraClick(). Now same mobile is having Panorama mode available in camera, so functionality would be same but with mode. This type is said to be Static polymorphism or Compile time polymorphism.

Compile time polymorphism the compiler knows which overloaded method it is going to call.

Compiler checks the type and number of parameters passed to the method and decides which method to call and it will give an error if there are no methods that matches the method signature of the method that is called at compile time.

Another point where in SendMessage was intended to send message to single person at a time but suppose Nokia had given provision for sending message to a group at once. i.e. – Overriding the functionality to send message to a group. This type is called Dynamic polymorphism or Runtime polymorphism.

For overriding you need to set the method, which can be overridden to virtual & its new implementation should be decorated with override keyword.

One thought on “OOPs with Swift and real life example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s