- The process of hiding internal data from the external world and accessing it only through publicly exposed methods is known as encapsulation.
OR
- Wrapping of data and member functions into a single unit so that we can provide security for the data
Example:
bank.java
class bank
{
private double balance; //by declaring balance variable as PRIVATE, we can
encapsulate
public void setBalance(double balance)
{
this.balance=this.balance+balance;
}
public double getBalance()
{
return balance;
}
}
customer.java
class customer
{
public static void main(String a[])
{
bank b1=new bank();
//b1.balance=4000;
compile time error ll occur, bcoz balance variable ENACAPSULTED by declaring as
PRIVATTE
b1.setBalance(4000) ;
//system.out.println(b1.balance); compile time error
System.out.println(b1.getBalance());
}
}
No comments:
Post a Comment