Java Encapsulation

Encapsulation
       Encapsulation is a mechanism of binding the data and function into a single unit. Encapsulation essentially has both information hiding and implementation hiding. Encapsulation provides a way for abstraction.

Example:

class Encapsulation
{
    //Restrict direct access, hiding Data
    private int a;
    private int b;
    
    public void setA(int a){
     this.a=a;
   }
     public void setB(int b){
       this.b=b;
   }
    //hiding implementation
    public int sum(){
        int total=a+b;
        //  Other Implementation
        return total;
    }
}

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation