Created
July 7, 2021 19:00
-
-
Save Gabbrolee/7f9dc344f64e0e42072c2c30ec2a2541 to your computer and use it in GitHub Desktop.
FOUR PILLARS OF OBJECT ORIENTED PROGRAMMING ( OOP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Introduction: | |
Object oriented programming is a programming paradigm base on the concept of object which can contain data and code. | |
Data: in the form of field (which often refer to as properties or attributes). Code: in the form of procedures (which often refer to as methods) Object: in the form of real world object or things eg cat , human , car , etc. | |
Code: in the form of procedures (which often refer to as methods) | |
Object: in the form of real world object or things eg cat , human , car , etc. | |
one: | |
Encapsulation: This is an object oriented programming concept that bind together the data and method( functions define inside a class ) that manipulate the data and keeping it private unless declared public. | |
code snippet: | |
class Account{ | |
var id: Int | |
var customerId: Int | |
var accountBalance: Int | |
var typeOfAccount: String { | |
return " " | |
} | |
var interestRate: Double{ | |
return 0.05 | |
} | |
init(id:Int, customerId:Int, accountBalance:Int){ | |
self.id = id | |
self.customerId = customerId | |
self.accountBalance = accountBalance | |
// self.interestRate = interestRate | |
} | |
//MARK:- withdrawal method implementation...this enables withdrawal | |
func withdrawal( _ amountToWithdraw: Int) -> String { | |
if amountToWithdraw <= accountBalance{ | |
accountBalance -= amountToWithdraw | |
print( "You have withdrawn successfully:#\(amountToWithdraw/100)") | |
return "\(accountBalance)" | |
}else{ | |
print("Opps! You have insufficient fund") | |
return "insufficient fund" | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Two: | |
Abstraction: This is a way of hiding all the implementation of a class from anything outside the class and the end user, | |
for instance object like phone could be consider. The button and input around it perform some underlying method, | |
which is of no business to the end user. | |
Three: | |
Inheritance: This is when a child class inherit properties of the parent class whilst still having it’s own unique properties. | |
code snippet | |
//MARKS:- child class of account created as saving account | |
class SavingAccount: Account{ | |
override var typeOfAccount: String{ | |
get{ | |
return " savingAccount" | |
} set{ | |
} | |
} | |
// override of interest rate is done here and it get the value, didnot set value since it is not needed | |
override var interestRate: Double{ | |
get{ | |
return 0.01 | |
} | |
set{ | |
} | |
} | |
// override deposit method to have attribute of parent class account called here with super as keyword | |
override func deposit(_ amountDeposited: Int) -> String { | |
super.deposit(amountDeposited) | |
super.bonus() | |
print("Congratulation! Bonus of #10 has been added to your deposit") | |
print("You have deposited successfully #\(amountDeposited/100) Current Balance:#\(accountBalance/100)") | |
return "\(accountBalance)" | |
} | |
// override withdrawal method to have attribute of parent class account called here with supper as keyword | |
override func withdrawal(_ amountToWithdraw: Int) -> String { | |
super.withdrawal(amountToWithdraw) | |
// print("You have withdrawn successfully #\(amountToWithdraw) Current Balance:#\(accountBalance )") | |
return "\(accountBalance)" | |
} | |
} | |
Four: | |
Polymorphism: Polymorphism is an object-oriented programming concept that refers to the ability of a variable, function or | |
object to take on multiple forms. In a programming language exhibiting polymorphism, class objects belonging to the same hierarchical tree | |
(inherited from a common parent class) may have functions with the same name, but with different behaviors. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well done...you could do better though.