What is encapsulation in java or oops?

 Encapsulation is a mechanism through which we can wrapping the data members(variables) and member methods(methods) of class in a single unit classed encapsulation.

The meaning of encapsulation is make sure that sensitive data is hidden or unchangeable from users to achieve this you must specify private keyword to variables 

public class Person{

private String name;

}

//Getter method for getting value

public String getName(){

return name;

}

//setter method for setting value to name vareable

public  void setName(String newName){

this.name=newName;

}

}

public class Main{

public static void main(String[] args){

Person   myObj =new Person();

myObj.setName("eaglewebworld");

System.out.println(myObj.getName());

}

}


Comments

Blogs