Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects

Sunday, 27 October 2013

Encapsulation in Java - Example and Explanation

In this particular blog we will look into a very useful concept in java i.e. Encapsulation. Encapsulation is one of the four (Abstraction, Encapsulation, Inheritance & Polymorphism) OOPS concepts in Java.

Encapsulation in Java – Definition and Explanation

When it comes to Encapsulation in java, we can simply define it saying:

Encapsulation is a practice to bind related functionality (Methods) & Data (Variables) in a protective wrapper (Class) with required access modifiers (public, private, default & protected) so that the code can be saved from unauthorized access by outer world and can be made easy to maintain.

We can achieve complete encapsulation in java by making members of a class private and access them outside the class only through getters and setters. Although a lesser degree of encapsulation can be achieved by making the members public or protected. Encapsulation makes it possible to maintain code that is frequently changeable by took that in one place and not scatter everywhere.

So, we can conclude our discussion saying that, Encapsulation is reflected with a well defined class having all related data and behavior in a single place and than access/restrict it using appropriate access modifiers.

Encapsulation Example in Java

Here is a sample code to demonstrate Encapsulation in java, Fruit class has all related data like name, taste, color.. etc and behavior like calculateCost in a single unit.
package com.beingjavaguys.core;

public class Fruit {

 private String name;
 private String price;
 private String taste;

 private Fruit(String name, String price, String taste) { = name;
  this.price = price;
  this.taste = taste;

 public void calculateCost() {


 public String getName() {
  return name;

 public void setName(String name) { = name;

 public String getPrice() {
  return price;

 public void setPrice(String price) {
  this.price = price;

 public String getTaste() {
  return taste;

 public void setTaste(String taste) {
  this.taste = taste;


Advantages of Encapsulation in Java

Encapsulation helps the developer to make the code more flexible and maintainable by binding related data in a single unit and access/restrict that using appropriate access modifier. With well encapsulation implementation, one can change one part of the code easily without affecting the other part of the code.

Difference between Encapsulation and Abstraction

This is one of the most frequently asked and discussed question in Java. Encapsulation and Abstraction are purely different scenarios.

Abstraction means to have a non-concrete manner of implementation either using abstract classes or interfaces. Abstraction means to define methods whose purpose is to be extended/implemented only. Think of an example of ‘Animal’ abstract class or interface, here we can define all possible behavior of an animal like, color, weight ..etc. Every animal will have these behaviors but the implementation will be particularly different for each class that implements or extends abstract implementation.

Here we are done with 'Encapsulation in Java - Example and Explanation'. In our upcoming blogs we will see more about Java Programming and other opensource technologies.

Thanks for reading !
Being Java Guys Team


  1. Thanks for your explanation..

  2. nice example.........and i understant can u give also how to data hiding is different form both

  3. great explanation yaar
    need your help im unable to fine overriding concept
    can u please give more real world exmaple

  4. rajesh adhikari18 July 2015 at 09:37

    nice explanation


Like Us on Facebook

Like Us On Google+


Skype: neel4soft