Example & Tutorial understanding programming in easy ways.

3. What is Encapsulation?

Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
                                     or
Encapsulation is an attribute of an object, and it contains all data which is hidden. That hidden data can be restricted to the members of that class.

Example:
 

public class EncapTest{

private String name;

private String idNum;

private int age;

public int getAge(){

return age;}

public String getName(){

return name; }

public String getIdNum(){

return idNum;}

public void setAge( int newAge){

age = newAge;}

public void setName(String newName){

name = newName;}

public void setIdNum( String newId){

idNum = newId;

}}

Read More →