Understanding abstraction

Abstraction is also a concept in object-oriented programming and implies that, when we write code, we should hide the complexity and details of the implementation from the outside world.

In other words, when we write a program that, on receiving an input, does a bunch of complex operations and returns an output, we should hide the inner complexity of the operations that are done inside the program so that the outer applications can just focus on the input they are sending to the application and the output that they are getting from it. 

For example, let's consider the same example of Account that we worked on in the previous example. If we consider the example of the OpenAccount function, you will understand that opening an account for the customer will not be that simple. There will be several subtasks that need to be executed before we can finally open the account for the customer. For example, some of the steps could be as follows:

  • Verifying the identification documents of the customer
  • Linking and opening different bank accounts, which could be Salary, Current, and Savings
  • Fetching, that is, counting the initial amount deposit of the customer

Basically, in real life, the function that we have written above will look more similar to the following code snippet. In OpenAccount, we are passing a Customer object. Before creating the bank account of the customer, we are doing three distinct tasks:

  1. VerifyCustomerIdentity(): In this function, the idea is to verify the identity of the customer, which is a common practice before an account is opened.
  2. OpenAndLinkRelatedAccounts(): In this function, the idea is to open different accounts for the same customer, that is, Savings, Current, and Salaried.
  3. RetrieveAndCountDeposit(): In this function, the idea is to retrieve the money the customer intends to save, count it, and finally deposit it in the customer's account:
public bool OpenAccount(Customer customer)
{
this.openingDate = DateTime.Now.Date;
this.currentBalance = 0.0f;
this.customer = customer;
if(VerifiyCustomerIdentity() && OpenAndLinkRelatedAccounts() && RetrieveAndCountDeposit())
{
return true;
}
else
{
return false;
}
}
private bool VerifiyCustomerIdentity()
{
//This function will verify the customer documents.
return true;
}
private bool OpenAndLinkRelatedAccounts()
{
//This function will open the related accounts of savings , current and salary and link them together.
return true;
}
private bool RetrieveAndCountDeposit()
{
//This function will fetch the deposit, count and verify the amount.
return true;
}
public bool DepositMoney(float deposit)
{
this.currentBalance = this.currentBalance + deposit;
return true;
}

Please note the following:

  • The three functions, that is, VerifyCustomerIdentity(), OpenAndLinkRelatedAccounts(), and RetrieveAndCountDeposit(), all of which have Private as the access modifier. This will ensure that the complexities in these three functions are not exposed to the outside. 
  • These three functions are being internally called in the OpenAccount function, so the calling application doesn't need to worry about calling these functions explicitly. 
  • Let's say we discover some issues in the internal private function. In this case, we can easily make changes in these internal functions without needing to worry about the external implementation.
..................Content has been hidden....................

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