Skip to main content

Java Encapsulation

Encapsulation is feature of object oriented programming that are used to binding function and data into single unit. This is provide a shield which are protect this data outside of class. Encapsulation are implemented in a Java programming by define all private data member of a class, And this properties are used by setup the public method of class.

class Employee{
    //Define private data of class
    private int empId;
    private String empName;
    private double amount;

    public void setId(int id){
        //Set private data value
        this.empId=id;
    }
    //Access the value of private data 
    public int getId(){
        return empId;
    }

    public void setName(String name){
        this.empName=name;
    }
    public String getName(){
        return empName;
    }
    public void setAmount(double amount){
        this.amount=amount;
    }
    public double getAmount(){
        return amount;
    }
}
class Operates{
    public static void main(String[] args) {

        Employee human = new Employee();
        //Set private data values
        human.setId(190);
        human.setName("Lee");
        human.setAmount(100000.43);

        //Retrieve private data values
        int id = human.getId();
        String name = human.getName();
        double amount = human.getAmount();
        //Access data member
        System.out.println("ID : "+ id);
        System.out.println("Name : "+ name);
        System.out.println("Amount : "+ amount);


    }
}

Interesting that every method are capable to access the private properties of class. public method are use to access outside of class by class object, or within the inherited class method.

In this example within the Employee class are define three private data member which are protected by class and never directly access outside the class. Member function are capable to access this private data values. They can setes new value and return existing values.

Set private data by public method

See this image, here setId() method are sets one private data value. But this method are capable to set and update other private data value. In this case this method are sets only one data value. Other data field contain default values (String is null and double is 0.0). This default value are modified by other two methods.

Set private name vlaue

In this stage sets the other data member value by setName(String name) method. This is public function of Employee class.

Set private amount vlaue

Observer human object of Employee are indirect access public data member value using of public method. And human object is capable to access this properties by using public method. So method is bind the private data and that is provide an interface to access this data. See the result of above program.

Output

ID : 190
Name : Lee
Amount : 100000.4




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment