Implementation-hiding

Another important concept to do with encapsulation is implementation-hiding. The idea behind implementation-hiding is to hide the inner workings of the function from the outside world. Let's take a look at an example to understand this concept.

In our everyday life, we meet and interact with many people. When we ask how someone is feeling or the name of their new pet, that person exhibits some behavior to provide the answers to our questions, such as saying I am fine, thank you! or My cat's name is Catty. The information about which language this information was stored in that person's brain and how they processed this information is hidden from the outside world.

Similarly, every smartphone contains an address book application that stores contact details and provides them when for us when we tap on the right place on the screen. How the mobile phone stores this information, whether it applies any encryption, and whether it loads the data in an array or in a list is hidden from us. Even if the inner implementation of the application completely changes, it wouldn't matter to us as long as the way in which we can access the information is the same.

A further example would be motor cars. All cars reduce their speed when the brake pedal is pressed. How does the brake system work—are they mechanical drum brakes, hydraulic brakes, or disk brakes? All these details are hidden from the outside world. We know that braking systems have evolved from simple to more complex mechanics, but we are unlikely to know any further information; everything is fine as long as the car reduces its speed when the brake pedal is pressed.

Let's create a bank account class. The simplest form of a bank account has two properties: the account ID and the current balance. It also has two behaviors—depositing money and withdrawing money:

class BankAcc(private val accID : Int, private var balance : Double){
fun withdraw(remove : Double) {
if(balance >= remove){
balance -= remove
println("$remove money has withdrawn from $accID account,
current balance is
$balance")
} else {
println("Not enough balance in $accID account,
current balance is
$balance")
}
}

fun deposit(add : Double){
if(add > 0){
balance += add
println("$add money has deposited in $accID account,
current balance is
$balance")
} else {
println("Cannot process....")
}
}
}

fun main(args: Array<String>) {

var account = BankAcc(123, 500.0)
account.withdraw(600.0)
account.withdraw(100.0)
account.deposit(600.0)
}

In the main function, we create an object of the BankAcc class with two initial values: the account number and the amount of money in the account. Next, the withdraw function deducts the money from the current balance, verifying the action before deduction. The function displays an error message if the withdrawal request is more than the current balance. We then have a deposit function, which allows us to deposit money in our account. We also verify that the deposit should not be a negative value.

Encapsulation can help here in the following ways:

  • All properties and functions are placed in one container
  • All properties are declared as private
  • Properties are restricted and cannot be accessed directly
  • The deposit and withdraw implementation is hidden from the outside world and all private properties can be accessed after verification
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.123.126