Home ProgrammingJava Encapsulation Concept with Java
Encapsulation

Encapsulation Concept with Java

by Log Raj
1735 views

I have already covered bout some of the OOP features like Inheritance in the series. Now It’s time for Encapsulation. It is most simple thing but bit difficult concept to depict. Name is big but after understanding this you will find it as a simple concept that changed the way of programming.

Encapsulation in java

Encapsulation is one of the main feature of object oriented concept. I want to make clear that this not a specification of any of programming language. It is a just literal concept therefor every object oriented programming language try to achieve encapsulatin in different ways. So we need to understand this term conceptually as well programatically.

As the name suggests, encapsulation means activity of putting different things together to form a single model. We can think this analogous to the drug capsule which contain different types of antibiotics mixed together to cure a diseases. So encapsulation is the process of wrapping data and code together to make a single unit.

In java or in most of the object oriented programming languages encapsulation is achieved using classes. Encapsulations also make possible data hiding by wrapping data and methods together. In the procedural languages there are only two types of data items, either local or global. Global are accessible from everywhere and locals accessible only from inside block. But in java encapsulatin allow multiple levels of encapsulation or more specifically data hiding. We can completely hide the data from outsiders or make read only or make write only or make data available for reading and writing both. So as per our requirement we can control the level of data hiding.

Example:

Lets go through below example and try to understand how to do encapsulation and what this is.

StudentInfo.java

package test;
public class StudentInfo {

private long internalId;
private String name;
private String comment;
public StudentInfo(long internalId, String name, String comment) {
super();
this.internalId = internalId;
this.name = name;
this.comment = comment;
}
public String getName() {
return name;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}

Now I want to point out each point that you need to understand while being here. First thing We put three data items and some methods together and this simple thing is Encapsulation. There might be more methods to do complex tasks.

Second thing With the help of encapsulatin we can hide the data from outsiders using access modifiers. For example no one can access or change the internalId of student. Or you can access the name of student but can not change his name but anyone can see comment and change comment.

Advantages of Encapsulation

Data hiding is one of the most important advantage. By using the concept of encapsulatin we can hide the details of data items. Although we are hiding the data but we can allow to access data using setter and getter methods.
As I explained earlier in this article We can have different level of data hiding. So encapsulation make data hiding and data security more flexible.
Encapsulation increase the re-usability of he code. Once you create a class,  it can be used to represent similar model in program.

Hope you understand what I tried to explain Please suggest or comment about content. You can comment below or mail us. happy learning!!

Encapsulation Concept with Java

by Log Raj time to read: 2 min
0