Ads Sponsored By Google :)

Saturday, 19 January 2013

define|definition|data|example for|of encapsulation vs Abstraction in java with real time|real life


encapsulation in java data encapsulation in java example for encapsulation in java encapsulation example in java encapsulation in java with example example of encapsulation in java abstraction vs encapsulation in java encapsulation vs abstraction in java definition of encapsulation in java encapsulation and abstraction in java define encapsulation in java encapsulation in java with real time example encapsulation in java example program encapsulation in java simple example encapsulation in java with real time example

   Encapsulation :

      
               Encapsulation is one of the object oriented programming principle in java which  
       enforce protecting variables, functions from outside of class, in order to better manage
       that piece of code and having least impact or no impact on other parts of program due 
       to change in protected code. Encapsulation in Java is visible at different places and
      Java language itself provide many construct to encapsulate members. You can completely  
      encapsulate a member be it a variable or method in Java by using private keyword and you 
      can even achieve a lesser degree of encapsulation in Java by using other access modifier  
      like protected or public. In real time true meaning of encapsulation is realized in an 
     environment which is prone to change a lot and we know that in software requirements 
     changes every day at that time if you have your code well encapsulated you can better 
     manage risk with change in requirement. Along with abstraction in java and polymorphism in 
    Java, Encapsulation is a very important concept in real time.

 Definition Of Encapsulation:

            Encapsulation is the technique of making the data members in a class as a private 
    and providing access to the data members through public methods. If a data member  is 
   declared as a private, it cannot be accessed by anyone outside the class, thereby hiding 
   the fields within the class. 
         For this reason, encapsulation is also referred to as data hiding.Encapsulation can be 
     described as a protective layer that prevents the code and data being accessed by other 
    code defined at outside of the class. Access to the data and code is tightly controlled by an 
    interface.

The Following Code or Program Illustrate the Concept of Encapsulation:

    class Empolyee{

    private int workduration;  //private variables examples of encapsulation
    private String hoursperday; //private variables examples of encapsulation
    private String salperhour; //private variables examples of encapsulation
    private String annualincome; //private variables examples of encapsulation  
  
     //public constructor can break encapsulation instead use factory method
    //so we are using private constructor here
  private Employee(int workduration, String Employee, String extrawork, String salperhour){
        this.workduration =work duration;
        this.hoursperday = hoursperday;
        this.salperhour = salperhour;
        this.annualincome = annualincome;
    }
      
   // create Employee can encapsulate Employee creation logic
    public Loan createEmployee(String EmpType){
 
     //processing based on Employee type and than returning Employee object
      return Employee;
    }
  
}

        In the above example regarding Encapsulation in Java you see all data member 
     variables are declared as private so they are well encapsulated you can only change
    or access these variables directly inside this class. if you want to allow outside world  
    to access these variables is better creating a getter and setter e.g. getEmp() and 
    setEmP() that allows you to do any kind of validation, security check before return
    Employee so it gives you complete control of whatever you want to do and single 
    channel of access for client which is controlled and managed.


   Advantage of Encapsulation in Java and OOPS

      Here are Some advantages while using Encapsulation in Java or any Object oriented    
   programming language:
1. Encapsulated Code or data is more flexible and easy to change with new requirements.
2. Encapsulation in Java makes testing so easy (i:e; Unit Testing).
3. Encapsulation in Java allows you to control (giving permissions to particular one’s what they require)  who can access what.
4. Encapsulation is helpful to write unchangeable or immutable class in Java which are particular useful in multi-threading concepts.
5. Encapsulation reduce coupling number of modules and increase sticking together inside a module because all piece of one thing are encapsulated in one place.

  What should you encapsulate in code
        Anything which can be change and more likely to change in near future is candidate of Encapsulation. This also helps to write more specific and cohesive code. Example of this is object creation code, code which can be improved in future like sorting and searching logic.

  Design Pattern based on Encapsulation in Java
Many design pattern in Java uses encapsulation concept. Some of the design patterns are

         1)      Factory Pattern
         2)      Singleton Pattern

       1)      Factory Pattern:
            Factory pattern is one of the design pattern in which it is used to create objects. Factory pattern is a good choice than new operator for creating object of those classes and whose creation logic can vary and also for creating different implementation of same interface.

For Example:
    BorderFactory class of JDK is one of the example of encapsulation in Java which creates different types of Border and encapsulate creation logic of Border.

      2)      Singleton Pattern:
           Singleton pattern is also one of the design pattern in Java. Which is also used to  encapsulate how you create instance by providing getInstance() method. Since object is created inside one class and not from any other place in code you can easily change how you create object without affect other part of code.

For Example:
                     Servlet is one of the Example such that for future aspect in Servlet Interface code is designed such for future developer defined classes object by using getInstance().

   Points To Remember About Encapsulation :
1. "Whatever changes encapsulate it" is a famous design principle.
2. Encapsulation helps in loose coupling and high cohesion of code.
3. Encapsulation in Java is achieved using access modifier private, protected and public.
4. Factory pattern , Singleton pattern in Java makes good use of Encapsulation.



Object Oriented Programming Principles Are :

1)      Class
2)      Object
3)      Data Abstraction
4)      Data Encapsulation
5)      Inheritance
6)      Polymorphism
7)      Dynamic Binding


1 comment:

LinkWithin