A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

What is Encapsulation?

What is Encapsulation?

What is Encapsulation?

  • Binding or wrapping code and data together into a single unit is known as encapsulation.

  • We can create a fully encapsulated class by making all the data members of the class private.

  • Java bean is the fully encapsulated class because all the data member are private here.

  • Here, we can use setter and getter methods to set and get the data in it.

  • In encapsulation, the variables of a class will be hidden from other classes because we set the data members(variables) private in encapsulation, and can be accessed only by the methods of their current class that methods will be setter method and getter method.

Read Only Encapsulation

Consider the following code as example of Adharcard and Mobile No. Registration to UID Center in India.

As, we all know that One Person can have one Adharcard and One mobile-no, registered with them. Here, user can read only adharcard, because adhar-center can generate only or write one time your adhar card and you will use or read multiple time any where from UID website.





Program for Read-Only mode

public class AdharCard_Registration 
{
 private String adharNo="429715551339";
 public String getAdharNo() 
 {
  return adharNo;
 }
}

public class TestEncap 
{

 public static void main(String[] args) 
 {
  AdharCard_Registration  myObject_Navneet = new AdharCard_Registration();
  //myObject_Navneet.setAdharNo("9898989898");  
  //we do not have setter method or we dont have permission to change card no.
  //adharNo="9898989898"; 
  //Card no is private, so we cant change outside;
  System.out.println(myObject_Navneet.getAdharNo()); 
  // we can read any where.
 }
}



 

Read and Write Encapsulation

In case of mobile, you can change multiple time as you want.





class MobileNoChange
{
 private String mobileNo;

 public String getMobileNo()
 {
  return mobileNo;
 }

 public void setMobileNo(String mobileNo)
 {
  this.mobileNo = mobileNo;
 }
 
 
}
public class MobileNo_Registration {

 public static void main(String[] args) 
 {
  MobileNoChange  noChangerObject=new MobileNoChange();
  noChangerObject.setMobileNo("9898777878");
  System.out.println(noChangerObject.getMobileNo());
 }

}



 

Write Only Encapsulation

In case of mobile, you can change multiple time as you want and cant read any where.





class MobileNoChange{
 private String mobileNo;
 public void setMobileNo(String mobileNo) 
 {
  this.mobileNo = mobileNo;
 }
 
 
}
public class MobileNo_Registration 
{

 public static void main(String[] args) 
 {
  MobileNoChange  noChangerObject=new MobileNoChange();
  noChangerObject.setMobileNo("9898777878"); 
  //you can write any where any time
  //System.out.println(noChangerObject.getMobileNo());
  //we cant read no, beacue this is read-only encapsulated mobile no
 }

}


 

For more knowledge about Encapsulation:-

To design effectively at any level of abstraction you need to be able to leave details of implementation behind and think in terms of units that group those details under a common interface.For a programming unit to be truly effective the barrier between interface and implementation must be absolute.The interface must encapsulate the implementation--hide it from other parts of the program.Encapsulation protects an implementation from unintended actions and inadvertent access.



In C a function is clearly encapsulated; its implementation is inaccessible to other parts of the program and protected from whatever actions might be taken outside the body of the function.Method implementations are similarly encapsulated but more importantly so are an object's instance variables.They're hidden inside the object and invisible outside it.The encapsulation of instance variables is sometimes also called information hiding.



It might seem at first that hiding the information in instance variables would constrain your freedom as a programmer. Actually, it gives you more room to act and frees you from constraints that might otherwise be imposed. If any part of an object's implementation could leak out and become accessible or a concern to other parts of the program it would tie the hands both of the object's implementor and of those who would use the object. Neither could make modifications without first checking with the other.



Suppose for example that you're interested in the Faucet object being developed for the program that models water use and you want to incorporate it into another program you're writing. Once the interface to the object is decided you don't have to be concerned as others work on it fix bugs and find better ways to implement it. You'll get the benefit of these improvements but none of them will affect what you do in your program. Because you're depending solely on the interface nothing they do can break your code. Your program is insulated from the object's implementation.



Moreover, although those implementing the Faucet object would be interested in how you're using the class and might try to make sure that it meet your needs they don't have to be concerned with the way you're writing your code. Nothing you do can touch the implementation of the object or limit their freedom to make changes in future releases. The implementation is insulated from anything that you or other users of the object might do.







class Employee
{
 private String name;
 private String address;
 private String email;
 private String pass;

 public String getName() 
 {
  return name;
 }
 
 public void setName(String name)
 {
 this.name = name;
 }

 public String getAddress() 
 {
  return address;
 }
 
 public void setAddress(String address)
 {
        this.address = address;
 }
 
 public String getEmail() 
 {
  return email;
 }
 
    public void setEmail(String email)
 {
        this.email = email;
    }
 
    public String getPass() 
 {
        return pass;
    }
 
 public void setPass(String pass)
 {
        this.pass = pass;
    }

    public static void main(String args[])
    {
        Employee obj = new Employee();
        obj.setName("Heera");
        obj.setEmail("heeraBabu@gmail.com");
        obj.setAddress("delhi");
        obj.setPass("nitu");

        System.out.println("my name is "+obj.getName());
        System.out.println("my email is "+obj.getEmail());
        System.out.println("my address is "+obj.getAddress()); 

    }

}




 

LEARN TUTORIALS

.

.