0% found this document useful (0 votes)
21 views6 pages

OOPS

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

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.

# Basic Concept of OOPS


Following are the concepts of OOPS,
1. Abstraction.
2. Encapsulation.
3. Inheritance.
4. Polymorphism.

# 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
}

class Animal: AnimalPlay {


let name: String
let age: Int

private var isFed: Bool = false

init(name: String, age: Int) {


self.name = name
self.age = age
}

func play() -> String {


return "Roll on the floor"
}

func makeSomeNoise() -> String {


return "sound~"
}
}

class Dog: Animal {


override init(name: String, age: Int) {
super.init(name: name, age: age)
}

override func makeSomeNoise() -> String {


return "Barrkkk~"
}

override func play() -> String {


return "Jump jump jump"
}
}

let popo = Dog(name: "Popo", age: 1)


let lolo = Dog(name: "Lolo", age: 3)

popo.play() // Jump jump jump


lolo.play() // Jump jump jump

# 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()
}

func sumOfArray() -> Int {


return self.arrayData.reduce(0, +)
}

func SortedArrayData() -> [Int] {


return sortedData
}
func smallestData() -> Int {
return sortedData.first ?? 0
}

func largestData() -> Int {


return sortedData.last ?? 0
}
}

let integerArray = ArrayData(array: [8,5,2,4,9,7])


let sortedData = integerArray.SortedArrayData()
let sumOfData = integerArray.sumOfArray()

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"
}

func validatePIN(_ pin: String) -> Bool {


if self.PIN == pin{
return true
} else {
return false
}
}
}

let customer = Customer()


customer.name = "Rakib"
let result = customer.validatePIN("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")
}
}

class Dog: Animal {


func display() {
print("My name is ", name)
}
}

var labrador = Dog()


labrador.name = "Rohu"
labrador.eat()
labrador.display()

# 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.

Only one method but acts differently. It’s called polymorphism.


class Shape {
var area: Double?

func calculateArea(valueA: Double, valueB: Double) {

}
}

class Triangle: Shape {


override func calculateArea(valueA: Double, valueB: Double) {
area = (valueA * valueB) / 2

print(area ?? 0)
}
}

class Rectangle: Shape {


override func calculateArea(valueA: Double, valueB: Double) {
area = valueA * valueB
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

init(name: String, pass: String) {


self.userName = name
self.passWord = pass
}

/// Abstraction
func registerUser() {
validateUser()
saveUserToDataBase()
}
/// Abstraction
func loginUser() {
...
...
return true // if data found in db
}

/// Encapsulation
private func validateUser() {
...
...
}

private func saveUserToDataBase() {


...
...
}
}

You might also like