Encapsulation is grouping of the properties and methods within a class.
Providing access to an object only through its messages,while keeping the details private is called information hiding.An equivalent term to information hiding is encapsulation.Encapsulation is a process that allows selective hiding of properties and methods in a class.The advantage of encapsulation is that a class can have many properties and methods but only some of these are exposed to the user.
In example of an organization there are many methods and properties of the organization that are classified as private.When a company B deals with company A,it does not interfere with the way in which Company A functions.Any information that Company B needs has to be strictly routed through proper channels such as,the public relations offer or administrative manager of Company A.
When code is properly encapsulated two objectives are achieved:
1. we build an impenetrable wall to protect the code from accidental corruption due to the silly little errors that we are all prone to make.
2. we also isolate errors to small sections of code to make them easier to find and fix.
Example of Encapsulation in Java:
private int sn;
private String empName;
private int empAge;
//Getter and Setter methods
public int getEmpSN()
public String getEmpName()
public int getEmpAge()
public void setEmpAge(int newValue)
empAge = newValue;
public void setEmpName(String newValue)
empName = newValue;
public void setEmpSN(int newValue)
sn = newValue;
public class ee
public static void main(String args)
EncapsDemo obj = new EncapsDemo();
System.out.println(“Employee Name: ” + obj.getEmpName());
System.out.println(“Employee SSN: ” + obj.getEmpSN());
System.out.println(“Employee Age: ” + obj.getEmpAge());
In above example all the three data members (or data fields) are private which cannot be accessed directly. These fields can be accessed via public methods only. Fields
empAge are made hidden data fields using encapsulation technique of OOPs.
Static and Dynamic Bindinding in Java
Binding: Connecting a method call to the method body is known as binding.There are two types ...read more
Polymorphism in Java
Polymorphism in java is a concept by which we can perform a single action by ...read more
Abstraction in Java
Abstraction is a process to hiding the implementation details and only showing the functionality to ...read more
Object in java
Object Object is a unique entity, which contains data and functions (characteristics and behaviour) together in ...read more
What is OOPs?
Object Oriented Programming (OOP) OOPs is an approach to standardize the programs by creating partitioned memory ...read more
Features and Advantages of OOps
Feature of OOPs It makes the complete program simpler by dividing it into a number of ...read more