Encapsulation in Object Oriented Programming

Encapsulation is a concept in object oriented programming which focuses on protecting the variable and function from outside the world in order to manage the piece of code in better way and produce least impact on other parts of the code. Read More about OOPS concepts, features of object oriented languages and their benefits.

Encapsulation supports the mechanism of binding of parameter and functions together. The binding of parameter and functions can be done in a class.

Important points about encapsulation

  • Through encapsulation it is possible to hide the internal details of how an object does something in a class. Encapsulation helps in resolving the problem at the implementation level.
  • With defining a class or structure it is possible to define the accessibility of the members (methods, properties, variables) to the code outside it. Encapsulation can thus help us control the interaction between objects
  • The data of an object can be used by another object without knowing the entire data. For example, an Employee object may have name, address, company, and department as its properties. If a third party object wants to use the Employee object, it can request the name and address for the employee without needing to know the company and department details of the object.

Thus a great benefit of encapsulation is that it can change the internal implementation of a class without altering the overall system.

Let us understand encapsulation with the help of example

Class College
//creation of private members
private String collegeId;
private String collegeName;
private String collegeLocation;
private Long collegeContact;

//creation of private constructor
private college( String collegeid, String collegeName, String collegeLocation,collegeContact)
this.collegeContact =collegeContact;

//create college can encapsulate inside loan creation module
Public College createCollege()
return College;

In the above code, we have made all the member type as private so that they are encapsulated and we can update and access these variables only inside the class and nowhere else.

Design pattern used based on encapsulation

1) Singleton design pattern
Singleton design pattern is used to create an object with help of method get instance (). Since the object is created inside one class and not from any other place in code you can easily change how you create object without affecting the other part of code.

2) Factory design pattern
Factory design pattern is used for creation of the design pattern which is used to create objects other than creating object through new operator.

Benefits of Encapsulation

  1. Encapsulated codes in object oriented programming are better and more flexible to use.
  2. Encapsulated codes in object oriented programming can be easily adaptable to new changes.
  3. Encapsulated codes can restrict the user access.
  4. Encapsulated code in object oriented programming reduces coupling of the modules and increases cohesion because all the codes are encapsulated together.
  5. If we change anything in the encapsulated code then rest part of code remains unaffected.
  6. Through encapsulation, we can change the implementation code without breaking the code of others
  7. In encapsulation we hide the implementation details behind a public programming interface.
  8. Encapsulation makes unit testing easy.

Read More :
Advantages And Disadvantages Of Peer To Peer Network
What are Proxy Servers and their uses ?
Benefits of Beta Testing

Copyright © ianswer4u.com

0 Reactions:

Post a Comment