07 August 2017

OOP - Encapsulation

Encapsulation binds properties and methods to a object defined by class.

Example:

Class Vehicle has private attributes and public getter and setter methods.

public class Vehicle {
    private String fuelType;
    private String capacity;

    public String getFuelType() {
        return fuelType;
    }

    public void setFuelType(String fuelType) {
        this.fuelType = fuelType;
    }

    public String getCapacity() {
        return capacity;
    }

    public void setCapacity(String capacity) {
        this.capacity = capacity;
    }
    
}

These attributes and methods bind to a object and object properties can be accessed via public methods.

"public","private","protected","default" are the access specifiers,which restrict the access of properties at various levels.Thus provides security to the object o the class while accessing.