The concept of abstraction

Abstraction is a key concept in the domain of Object-Oriented programming as well as the understanding of data structures and algorithms. The dictionary meaning of Abstract refers to something that exists only in idea but not in physical or realistic existence. It is from this basic understanding that I'll attempt to explain the concept of abstraction from a real-life scenario.

Abstraction is a process where we show only relevant data and hide unnecessary details of an object from the user. For example, Consider the remote control of a TV, you as the user only know which key to press for what function. You will not know what happens inside when you press whichever button. This is the kind of scenario that happens in Java. Abstraction is achieved with the help of interfaces. In this case, the remote control of the TV is an interface. It these interfaces that expose only methods to end users with a view to reducing complexity and increasing efficiency of using the object.

We can enhance the internal implementation without effecting outside world. Abstraction provides security. A class contains lot of data and the user does not need the entire data. The advantage of abstraction is that every user will get their own view of the data according to their requirements and will not get confused with unnecessary data.

When we hide unnecessary details of an object from a user, this is described as data hiding, which is a process of keeping out internal data safe from un-authorized access. This means no one can access our internal data directly.

In my last blog post, I demonstrated how we can achieve data hiding programatically by declaring data members as private, so that an outside person (or object) can access our data through public methods using getters and setters.

Real-life example

Let's say for instance you are a member of a bank in which many other people's accounts are there. There is some banking software which has been implemented in such a way that the bank balance variable is defined as a public variable. This means that if you are having an account in this bank, then your account balance is somehow known to the public, so any one can know your account balance. Ideally, no one would like this because one's bank balance is a private piece of info.

So in order to make your account safe such banking software declares the balance variable as a private variable so that anyone who wants to access this private data can only do so through methods defined inside that class, and this method will be probably asking the account holder to input his/her account number, as well as a PIN number for authentication purposes.

So by utilizing the concept of data hiding,security is achieved. This is one of the mechanisms through which encapsulation works.

In summary, encapsulation can be loosely defined as the wrapping up of data into a single unit. Abstraction on the other hand refers to selecting only the necessary details from the given data without compromising on the functionality. Encapsulation allows abstraction since they work hand in hand. Encapsulation itself is a subset of abstraction by hiding what you don't need while abstraction shows you what you need.