Home » Java programming language

What are the differences between abstraction and encapsulation?

Learn: Here, we will learn what is the difference between abstraction and encapsulation in java? What are the similarity and dissimilarity of abstraction and encapsulation in java? Compare abstraction and encapsulation in java?
Submitted by Preeti Jain, on February 01, 2018

Abstraction in java

1) Abstraction is an object oriented programming concepts which is also introduced in java.

2) Abstraction means we are hiding internal implementation details. (Or in other words highlighting the set of services what they are offering).

Example 1:

ATM USER INTERFACE:
Highlighting the set of services what ATM are offering like CASH WITHDRAWL, BALANCE ENQUIRY, and PIN CHANGE etc. But we don't know about internal coding behind the services (i.e. Hidden internal implementation).

Example 2:

CAR DRIVER:
He knows how to start the car, how to change gears of the car but he does not know start process of the car and gears changing process of the car.

3) There are few advantages of Abstraction are:

  1. Security: Outside person does not know about internal implementation details by which we can achieve security.
  2. Enhancement: Without affecting outside person ( end user) we can change our internal implementation details if required so enhancement will be easy.

2) Encapsulation in java

1) Encapsulation is an object oriented programming concepts which is also introduced in java.

2) Wrapping up of data and their corresponding method into a single unit (or in other words the process of binding data and their corresponding method in to a single unit).

Example:

class GetAccountDetails{
	private double balance;

	public double getBalance(){
		return balance;
	}
	
	public void setBalance(double bal){
		balance = bal;
	}
}

class AccountDetails{
	public static void main(String[] args){
		GetAccountDetails GAD = new GetAccountDetails();
		GAD.setBalance(10000.000);
		double balance = GAD.getBalance();
		System.out.println(balance);
	}
}

Here, GetAccountDetails is a single unit where balance is a data member and getBalance() and setBalance are methods or member functions.

3) There are few advantages of Encapsulation are:

  1. We can achieve security.
  2. Enhancement will be busy.
  3. It improves maintainability of the application.

4) Disadvantage of Encapsulation is slow down execution due to security wise level check so performance gets down.

Read more:



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.