What is abstraction in java oops? – Easy way

Abstraction in Java is just a concept, and abstraction meaning is “Provide necessary information’s only to clients/users and hide unnecessary details. As an abstraction example, consider a login windows where only User Id and password is enough for users and we don’t need to give responsibilities to users to connect database and validate if credentials are correct or not etc.

So, only User Id and Password fields to users should be visible and database connection and validation should be hidden or abstracted from users.

Let’s understand the abstraction in java with simple and pseudo program example by implementing the above scenario.

There is a class LoginWindow that will allow client class or main() program to create object of its own with user id and password information and submit() method only and rest of information e.g. internal methods like VerifyCredential() and DBConnection class will be abstracted from client class.

Below is the program example of java abstraction in oops with comments.

 

 

Here is the client or main program that will have or access only required information i.e. login window to fill user id and password and submit button. Rest of internal information of login window class will be hidden.

 

 

NOTE:

In above abstraction concept, we have provide only required information to main() program and have hide the information e.g. internal methods and classes.

To hide the complexities we have adapted some mechanism in above program e.g. we have hide userid , password variable, data base class and VerifyCredential() methods  by making them private using private modifiers

The way of hiding complexities / information are known as encapsulation. In oops whatever way you can apply to hide information, all are known as encapsulation.

Read another Java interview question What is encapsulation in java object oriented programming

As a final note,

Abstraction in java: Provide only necessary information to client.

Encapsulation: Whatever the way we apply to hide information is encapsulation.

“In fact, implementation of encapsulation is Abstraction”

Read More