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.

//Login window class where 

class LoginWindow {

	/*-------------------------------------------------
	 * Only methods visible to User/Client program.
	 * Client/User can be main() program or other classes
	 */

	public LoginWindow(String userId, String pwd) {
		this.userId = userId;
		this.pwd = pwd;
	}

	public void submit() {
		VerifyCredential();
	}

	/*--------------Internal purpose only-----------
	 * Internal private methods variable and classes
	 * Not visible or accessible out side of login window
	 * class.
	 * 
	 */

	private String userId;
	private String pwd;

	private DBConnection connection;

	private void VerifyCredential() {

		this.connection = new DBConnection();
		this.connection.DBConnect();
		this.connection.verifyUser(this.userId, this.pwd);

	}

	/*------------------------------------------
	 * Nested private Database class not visible
	 * outside of login Window class
	 */
	private class DBConnection {

		public void DBConnect() {
			System.out.println("Connecting Database...");
		}

		public void verifyUser(String UserId, String pwd) {
			System.out.println("Verifing user...");

		}

	}

}

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.

/*------------------------------------------
 * Client class or main function  
 */
public class Abstraction {

	public static void main(String[] args) {

		// Put user id and password to login
		LoginWindow lw = new LoginWindow("User1", "pwd123");
		lw.submit();

	}

}

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”

Related Posts