Object Oriented Programming with Swift
In every programming interview you might have often came across a question by recruitter about object oriented programming and explain it in real world exmple. As per my experience in academic we learn all the concepts and how it works but when it comes to real world example being a fresher or straight out of college we may get stuck with this question.
Lets try to solve the mystry of OOP.
Q. What is Object oriented programming?
The paradigm of object oriented programming focuses on binding data and function together. In object oriented programming we have class, attributes, functions and objects.The class is blueprint of Object. Objects are instances of class created with defined data (attributes). Functions are defined inside the class which describes how the object is going to work or operate. Attributes are the instances of the class they are declared by class and defined by objects.
In the simple terms we can say that object oriented programming uses classes and objects to break down the complex logic of code into simple reusable pieces.
Q. Give an example of Object Oriented Programming in real world scenario?
Let us consider an example of website selling cars.
For example consider we are on CarWebsite that sells cars. We want to purchase a car. So we provide them information like name and budget. Based on the budget they provides a full list of cars that their customer might be interested in.
Here for understanding we have taken a swift code with class CarWebsite which has attributes or properites name and budget and function getDetails() which processes the data.
There might be thousends of customers like us who submits a request to purchase a car. So each customer can be treated as an object and provide them personalised servie based on their dynamic requests.
When we initaite an object with custom data. The instance of class is created with the object data.
For example for the user1 the values are name:”Akshay” and budget: 1000 so the getDetails will return the cars with price 1000 and below. The user2 object provided values as “Devkate” and budget: 2000 so the getDetails function will return the cars with price 2000 and below.
Q. Explain the terms Inheritance, Abstraction, Encapsulation and Polymorphism in OOP ?
These are four core principles of OOP
Inheritance in OOP helps to eliminate the code redundnacy that is writting same code function again and again. To eliminate code redundancy in OOP we inherit the properties and methods of parent class to the child class. The Parent class is the one whose attributes and methods are inherited. The Child class is the one that inherits the properties from the parent class.
There are different forms of inheritance in OOP they are single inheritance, multiple inheritance, multi level inheritance, hybrid inheritance, hierarchical inheritance.
- Single inheritance
A class inherits only one parent class in this type of inheritance. This is the most basic type of inheritance, and it’s also known as single inheritance.
2. Multiple inheritance
When a class inherits from more than one parent class, the inheritance becomes multiple inheritances. After inheriting properties from several parent classes, the child class has access to all of their objects.
Because of historical issues such as the deadly diamond and other ambiguities, Multiple Inheritance is constrained in Swift and many other languages by the usage of classes. Protocols in Swift can be used to accomplish multiple inheritance on some levels.
3. Multi level inheritance
Multiple inheritance occurs when a class has multiple child classes (sub classes), or when more than one child class has the same parent class.
Child Class is inherited by ParentB class which is inherited by ParentA class.
4. Hybrid inheritance
The term “hybrid inheritance” refers to when more than one type of inheritance is combined.
Multiple inheritance and multilevel inheritance are combined in hybrid inheritance. Multiple inheritance is not supported in Swift or many other programming languages since it causes ambiguity, hence this type of inheritance can only be achieved by using interfaces.
Abstraction refers to the quality of dealing with ideas rather than events. It essentially entails hiding the details and only displaying the most important information to the viewer. For example we just need to see the balence of our account in our mobile app rather than seeing the http request and code that goes to fetch the current balance of your accout. Abstraction is powerful mechanism or design to hide all the unnecessary information and just display the necessary information to the user.
Encapsulation is often reffered to information hiding in OOP. This also ensure hiding data and method getting modified from the outside intervention or methods.
for example- Consider the following code
With the above example we can see that the variable or attribute defied as private to achieve encapsulation which cannot be accessed outside of the class EncapsulationSwift. But it can be accessd by the functions or methods within its own class.
Polymorphism is inextricably linked to OOPs and is a fundamental notion in all object-oriented programming languages. In general, an object or reference can take on various forms in different contexts. Polymorphism as a whole would mean ‘a property of having many forms,’ as the name suggests ‘poly’ means ‘many’ and ‘morph’ points at ‘forms’.
There are two types of polymorphism in swift.
- Run time polymorphism
Method overriding is what runtime polymorphism is all about.
Swift uses runtime polymorphism to call the real method while the program is running.
2. Compile Time Polymorphism
During compile time, it will have no idea which method to call.
It is used in conjunction with inheritance.