Tuesday, June 28, 2016

Encapsulation

Encapsulation is the most important concept of Object oriented programming.

Encapsulation is the process of biding data and corespondent methods into a single unit and keeps safe from outside classes.



Example :

package example;

public class Person {
    
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    


package example;
public class Main{
public static void main(String args[]){
       Person person=new Person();
        person.setName("indrajeet Singh");        
        System.out.println(person.getName());
    }
}

in above example name property declared as private. So it could not be accessed directly outside the class.
 So that by using Setter and Getter methods name property or data member is accessed outside class.


Note : If any component follows data hiding and abstraction concepts, such type of component said to be Encapsulation component.

Encapsulation = data hiding  + abstraction



Advantage.
1. Encapsulation is also important concept  for security purpose.
2. It provides you control over the data which is declared as private.


It increases length of code So it takes time to execute the code.

<<       >>