encap 1 - Encapsulation in Java

Encapsulation in Java

Posted on

Definition : Encapsulation in java is a technique of wrapping code i.e knowledge (Variables) and and processes carried out on that knowledge i.e strategies collectively right into a single unit known as class. For instance capsule i.e. blended of a number of medicines.

encap - Encapsulation in Java


  • In encapsulation, the variables or knowledge of a category is hidden from every other class and might be accessed solely by any member operate of personal class by which they’re declared.
  • We are able to additionally use getter and setter strategies to get and set knowledge in it.
  • As in encapsulation, the information in a category is hidden from different courses, so it’s also often called data-hiding.
  • Encapsulation might be achieved by: Declaring all of the variables within the class as non-public and writing public strategies within the class to set and get the values of variables.


Instance :


 public class Encapsulation {

non-public      String title;

non-public      int rollno;


String getName() {

return title;


void setName(String title) {

this.title = title;


int getRollno() {

return rollno;


void setRollno(int rollno) {

this.rollno = rollno;


public static void important (String[] args){

Encapsulation e = new Encapsulation();








   Output : nandini