ID: 191-35-393 Department of Software Engineering Introduction
Encapsulation is the technique of making the fields in
a class private and providing access to the fields via public methods. Encapsulation also can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. A view of Encapsulation Example of Encapsulation File: Student.java File: Test.java // // A Java class which is a fully encapsulated class. A Java class to test the encapsulated cl ass. // It has a private data member and getter and se package com.javatpoint; tter methods. class Test{ package com.javatpoint; public static void main(String[] args){ public class Student{ // //private data member creating instance of the encapsulated private String name; //getter method for name class public String getName(){ Student s=new Student(); return name; //setting value in the name member } s.setName(“Dipu"); //setter method for name //getting value of the name member public void setName(String name){ System.out.println(s.getName()); this.name=name } } } } Output Dipu Importance of Encapsulation To hide the internal implementation details of the class Can safely modified the implementation without worrying breaking the existing code that uses the class Protect class against accidental/ willful stupidity Keeps class tidy by keeping the visible fields to a minimum Easier to use and understand Why need Encapsulation?
In object-oriented programming data encapsulation is concerned with-
I. Combining data and how it's manipulated in one place : This is achieved through the state (the private fields) and the behaviors (the public methods) of an object. II. Only allowing the state of an object to be accessed and modified through behaviors: The values contained within an object's state can then be strictly controlled. III. Hiding the details of how the object works: The only part of the object that is accessible to the outside world is its behaviors. What happens inside those behaviors and how the state is stored is hidden from view. Benefits
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 the code. Advantages
The fields of a class can be made read-only or write-
only. A class can have total control over what is stored in its fields. The users of a class do not know how the class stores its data. A class can change the data type of a fields, and a users of the class do not need to change any of their code . Facililities By providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods. It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods. It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members. The encapsulate class is easy to test. So, it is better for unit testing. Maintainability
Encapsulation draws a boundary around a set of data
and method. This allows a developer to use the code without having to know how it works, but with what input data and its input data range and with what it returns. Flexibility
Encapsulation makes the code easier to visualize.
This means codes can be arranged “visually” before implementing. Extensibility
Makes long term development easy.
Updates can be made by changing the encapsulated part without changing the input and output formats. Conclusion Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. The main benefit of encapsulation is the ability to modify the implemented code without breaking the code of others who use the implemented code. Encapsulation makes the programming code flexible, maintainable and extensible. Thank You