OOPS
OOPS
OOPS
# What is OOPS
OOPS is abbreviated as Object Oriented Programming System in Which programs are
considered as a collection of objects. Each object is nothing but an instance of a class.
# Abstraction
Abstraction is a process of exposing necessary functionality and hiding unnecessary
functionality.
We can achieve abstraction using Access Specifiers.
It shows only the necessary details to the client of an object. Meaning it shows only required
details for an object, not the internal constructors, of an object. Example - When we want to
switch on the television, it is not necessary to know the inner circuitry/mechanism needed to
switch on the TV. Whatever is required to switch on TV will be shown by using an abstract class.
protocol AnimalPlay {
func play() -> String
}
# Encapsulation
Encapsulation is a process of binding instance variables and methods together into a single unit
to keep safe from outside of the class.
Encapsulation is achieved through an access modifier.
class ArrayData {
private var arrayData: [Int] //= [4,19,9,12,6,8,14,4]
private var sortedData: [Int] //= []
init(array: [Int]) {
self.arrayData = array
sortedData = self.arrayData.sorted()
}
Using the object of ArrayData class we get sorted Array data but don’t know how data is sorted.
Another example:
class Customer {
var name: String!
private var PIN: String!
init() {
self.PIN = "1234"
}
# Inheritance
Inheritance is a concept where one class shares the structures and behavior defined in another
class.
If Inheritance applied to one class called single Inheritance, and if it depends on multiple
classes, then it is called multiple Inheritance.
Swift doesn't allow us to declare a class with multiple base classes or superclasses, so there is
no support for multiple inheritance of classes. A subclass can inherit just from one class.
However, a class can conform to one or more protocols.
Inheritance is the process of extending the existing class. Deriving the existing class
functionality and adding additional functionality to the newly created class.
class Animal {
var name: String = ""
func eat() {
print("I can eat")
}
}
# Polymorphism
Polymorphism is nothing but assigning behavior or value in a subclass to something that was
already declared in the main class. Simply polymorphism takes more than one form.
Polymorphism is nothing but the behavior of an object/method which acts differently based on
the context where it is being used.
}
}
print(area ?? 0)
}
}
# Abstraction vs Encapsulation
*Abstraction hides the internal implementation and creates the skeleton of what is required for
that entity.
*Encapsulation hides the data from the external world. It protects the data within class and
exposes some methods to the world which are expossable.
class User {
/// Encapsulation
private var userName: String
private var passWord: String
/// Abstraction
func registerUser() {
validateUser()
saveUserToDataBase()
}
/// Abstraction
func loginUser() {
...
...
return true // if data found in db
}
/// Encapsulation
private func validateUser() {
...
...
}