About Me

My photo
"Enoughtheory.com" had its humble beginning in the year 2011 by ( Founder of Enoughtheory.com ) Mr Ravi Kant Soni , an Enterprise Java and Spring Framework Specialist, with a bachelor degree (B.E) in Information Science and Engineering from Reva Institute of Technology at Bangalore. He has been into the software development discipline for many years now. Ravi has worn many hats throughout his tenure, ranging from software development, designing multi-tenant applications, integration of new technology into an existing system, to his current love of writing a Spring Framework book. Currently, he is a lead engineer at HCL Technology. Ravi has focused on Web and Enterprise development using Spring Framework for most of his career and has been extensively involved in application design and implementation. He has developed applications for Core-Bank, HR and Payroll System, and e-Commerce systems using Spring Framework. Ravi Kant Soni is author of book "Learning Spring Application development" http://learningspringapplicationdevelopment.com

Friday 14 August 2015

Encapsulation - the first Pillars of OOPS

Introduction 

Encapsulation is the first pillar or principle of object-oriented programming. “Encapsulation is a process of binding data members (variables, properties) and member functions (methods) into a single unit”. And Class is the best example of encapsulation.  

Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Encapsulation is the packing of data and functions into a single component. Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class.

  • Through encapsulation a class can hide the internal details of how an object does something. Encapsulation solves the problem at the implementation level.
  • With the help of encapsulation, a class can change the internal implementation without hurting the overall functionality of the system.
  • A class or structure can specify how accessible each of its members (variables, properties, and methods) is to code outside of the class or structure. 
  • Encapsulation simplifies the interaction between objects. An object can use another object without knowing all its data or how its data is maintained. For example, a Client object might have name, address, company, and department properties. If a Bank object wants to use a Client object, it can request the name and address for the bank without needing to know the company and department details of the Client object.  
 The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

Why do we encapsulate?

  •  To hide and prevent code (data) from the outside world (here the world means other classes and assemblies).
  • To prevent code (data) from accidental corruption due to programming errors so that we can deliver expected output. Due to programming mistakes, code may not behave properly and it has an effect on data and then it will affect the functionality of the system. With encapsulation we can make variables, properties, and methods private so it is not accessible to all but accessible through proper channels only to protect it from accidental corruption from other classes.
  • To have a class better control over its fields (validating values etc…).

Encapsulation in Java

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared 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.

Example  

public class Class {
    private String message;
    public void setaString(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}

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.

Advantage of Encapsulation in Java 

  • Encapsulated Code is more flexible and easy to change with new requirements.
  • Encapsulation in Java makes unit testing easy.
  • Encapsulation in Java allows you to control who can access what.
  • Encapsulation also helps to write immutable class in Java which are a good choice in multi-threading environment.
  • Encapsulation reduce coupling of modules and increase cohesion inside a module because all piece of one thingare encapsulated in one place.
  • Encapsulation allows you to change one part of code without affecting other part of code.

Design Pattern based on Encapsulation in Java 

Factory pattern is one of the design pattern in Java uses encapsulation concept which is used to create objects whose creation logic can vary and also for creating different implementation of same interface.

Why Getters and Setters? 

Why we need accessor and mutator methods in Java, why can’t we just access the data directly? But the purpose of encapsulation here is is not to hide the data itself, but the implementation details on how this data is manipulated. So, once more what we want is a way to provide a public interface through which we can gain access to this data. We can later change the internal representation of the data without compromising the public interface of the class. 

  • You can limit the values that can be stored in a field (i.e. gender must be F or M).
  • You can take actions when the field is modified (trigger event, validate, etc).
  • You can provide thread safety by synchronizing the method.
  • You can switch to a new data representation (i.e. calculated fields, different data type)