Encapsulation in Java Programming

This video tutorial teaches you the concept of Encapsulation in Java with the help of example code.

Encapsulation in Java - One of the 4 OOPs concept

In this video tutorial I will explain you the Encapsulation concept of the Java Programming language. Encapsulation is one of the 4 OOPs concepts and other three concepts are Inheritance, Polymorphism and the Abstraction.

This section explains you about the features of the encapsulation in Java and teaches you how to create the encapsulated classes.

Encapsulation is one of the four principals of the OOPs ( Object Oriented programming). The Encapsulation hides the variables of the class from getting accessed from outside and prevents its from accidental modification.

  • Encapsulation is one of OOPs concepts.
  • It's a way of hiding the data and its implementation
  • All the variables of the class are private and data can be accessed only through public methods
  • Functionality of the class is also hidden and only accessible through public methods

Encapsulation in Java - One of the 4 OOPs concept

Encapsulation wraps the data and functions in a unit (class). The data of the Encapsulated class can only accessed through public methods.

Nobody can access these private data outside the class so you can say encapsulation is like data hiding.

Video tutorial of Encapsulation in Java Programming:

Advantage of Encapsulation

  • Hiding the data and implementation logic.
  • Easy Unit Testing
  • Access control of the functions and data
  • Maintainability of the code
  • Organizing both code and Logic in one class
  • Code of the class can changed without affecting other parts of the application

Example code of Encapsulation

Here is the code of EncapsulationExample.java file:

package net.roseindia;

public class EncapsulationExample {
	private int roll;
	private String name;

	public int getRoll() {
		return roll;

	public void setRoll(int roll) {
		this.roll = roll;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;
	public int findGrade(){
		//any code for calculating the Grade
		//write logic for calculating grade
		return 1;

Here is the code of EncapsulationMains.java file:

package net.roseindia;

public class EncapsulationMains {
	public static void main(String[] args) {
		EncapsulationExample example = new EncapsulationExample();
		example.setName("Deepak Kumar");
		System.out.println("Roll No : " + example.getRoll());
		System.out.println("Name : " + example.getName());
		System.out.println("Grade : " + example.findGrade());

Download the source code in Eclipse project format.

Check all the Video tutorials of the Java Programming Language.

Share on Google+Share on Google+

Encapsulation in Java Programming

Posted on: January 4, 2015 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Encapsulation in Java Programming  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image