Abstraction in Java

By | July 26, 2020

Abstraction is the process of hiding the internal implementation details from the consumer by exposing necessary functionality.

Now let’s try to understand with Real World Examples.

Real World Example

Consider an ATM Machine, there are so many logics that have been implemented behind the scenes such as when you input your PIN,and once you click on the enter button, then inside that some validation logic has been triggered.

And once your pin validation has completed then accordingly next screen appears in front of you. It can either be the successful or validation failed message will appear. 

Here is the catch,  Who is doing all these validations?? How validation has been processed??

It’s obvious that these type of question arises in our mind, we all aware there must be something happing behind the scene but it is completely unknown to us how it’s working. 

At the same time, we know what is that logic doing, but don’t know how it is doing. Right?? Of Course Yes, here we know ATM is doing validation but don’t know how it is doing.

How that works,  is completely hidden from us, this concept is only called Abstraction.

Now again focus your concentration on Abstraction definition, so that you will get a clear idea. 

Abstraction is the process of hiding the internal implementation details from us by showing the necessary functionality.

Here in ATM Machine, internal implementation details is logic written to validate your PIN to verify whether it is correct or wrong.

Necessary Functionality refers to you can enter and see on screen your PIN and withdrawal amount.

I hope you got some idea of how abstraction is implemented in our day to day life. There are lots of other real-world examples out there such as Vehicles, in that you know it is used for driving but you don’t aware about how all mechanism is working on a deeper level.

Real-Time example in Java

Consider an online portal having a login form,  say Facebook. When you press on the Login button after filling username and password, there are lots of processes happens behind the scene.

Below I explain each step in detail.

The following are the things that happened behind the scene when you press on the Login button, that is hidden from common users like us.

Step 1:- Whatever you see on the login screen it’s called UI (User Interface), which consists of so many web elements such as Login Button, Input text area where you enter your username and password. 

Every web element has some functionality, that functionality we indicate with functions or method in code level and every method has some implementation or logic written in that.

Since Login Button is also a web element and there must be one dedicated method written for this. So, once you press the Login button, it takes the username and password, and which passes as an argument to that method.

Step 2:- Method for Login Button must have some logic written inside, that is it is written a line of code with established the connection with Facebook Database.

Step 3:- Technically the concept which used for established connectivity with Database is called JDBC.

The acronym of JDBC is Java Database Connectivity.

Once the connection gets established, the data received from UI such as username and password will get validated or verified with the data present inside the database. 

Step 4:- Once data get verified, it will return a message along with next page URL, if username and password are wrong then, it will return error page or simply an error message that is username or password may be wrong, on the other note if your credentials are correct then it will return the URL of your Facebook Homepage. 

Have you noticed there are lots of things happening behind the door, usually it is hidden from users, they simply see the homepage or login page

That whole concept from Step 1 to Step 4 which is hidden from the consumers or users is nothing but Abstraction. Please refer below image for better understanding.

In the above image, once you press the logic button, the request will go the Login method which is bind with that button, inside that method all business logic has been written such as doing validation once the connection established with the database and many more.

All the things are hidden from you. That’s called Abstraction in Java.

Now Let’s talk in a more practical manner. In Java  Abstraction can be achieved by using an abstract method that is either by using abstract class or else by using an interface.

If you don’t know about the abstract method then no need to worry, I briefly explain it below.

Abstract Method 

The method which has only method declaration without any method definition or implementation is called Abstract Method.

We only declare the abstract method either an abstract class or Interface.

Abstract Class

Abstract Class is a simple java class that contains at least one abstract method.

Basically in Java, methods are categories into two types.

  1. Abstract Method
  2. Concrete Method

Abstract Method: Method having the only declaration which starts with the abstract keyword as shown in the below figure. 

Generally is any method is the combination of two things that are,

  1. Method Declaration
  2. Method Implementation 

And in the abstract method, we only have method declaration, there is no implementation or logic.

Interface

It is a rule repository or coding contact which only contains abstract methods.

Important Points About Abstraction

  • In Abstraction, we simply hiding the unwanted logic from the customers and exposing only the functionality which is useful for them.
  • Abstraction can achieve with help of abstract methods, That abstract can be declared inside the abstract class or interface. 

Thank You so much for reading, I hope you might get a chance to learn something new and understood Abstraction in a better way. If You have any queries or doubts, please feel free to ask in below comment box. I love to answer your queries.

You can also check out my other articles.

Leave a Reply

Your email address will not be published. Required fields are marked *