Encapsulation in OOP (Object-Oriented Programming) is everything (data-attributes / behaviors- method or function) combining and keeping together as a single unit. This is used in the implementation level Object-Oriented Programming.
Data hiding is part of the encapsulation. let’s try to understand what is encapsulation in Java, examples of encapsulation in Java, advantages of encapsulation in Java, and abstraction vs encapsulation in Java in the next sections.
Encapsulation Real-Life Example
Example 1
Let’s take a car as an example. When considering the implementation of the car there are accelerated paddles, steering wheels, and gear-changing handles used to control the car.
There is an upper and lower level limit for speed when accelerating. Also, gear levels are selectable. The steering wheel has maximum rotation. That means all the above access points for the user but there are limitations and validation when implementing the car.
According to the example, we can see everything bundled into a single unit with user access points. All validation is done inside the car according to the user input. That will help to improve the security and safety of the car.
Examples 2
Let’s take another example of Encapsulation in the Real World. Consider a Bank Account. The reality is no one is able to directly set account amounts from outside. Account-holders are able to only money deposit and withdraw. Then the amount calculation will be hidden and show the remaining amount. When considering the security of a bank account encapsulation is a good programming technique to protect data from outside.
Encapsulation Real-Life Example with Java
Achieving Encapsulation in OOP in Java by using access modifiers. Private attributes are used for data hiding. Those data states are unable to access directly. Create getters and setters to access data from outside the Encapsulation.
// AutoGearCar.class class AutoGearCar{ private double speed; private String gearLevel; enum CarStates{TURNOFF,TURNON}; private CarStates carState; public AutoGearCar(CarStates carState) { this.carState = carState; } public double getSpeed() { return speed; } public String getGearLevel() { return gearLevel; } public CarStates getCarState() { return carState; } // Externally not possible to access data directly. // There for have to access by Setter. // when setting values inputs are validate internally. public void setSpeed(double speed) { if (speed<0 || speed >300 ||getCarState() == CarStates.TURNOFF ){ System.out.println("Car Speed Error"); }else{ this.speed = speed; } } public void setGearLevel(String gearLevel) { if (getCarState() == CarStates.TURNOFF && gearLevel == "D"){ System.out.println("Car gear chenge to drive mode not allowed. because its not started yet"); }else{ this.gearLevel = gearLevel; } } public void setCarState(CarStates carState) { this.carState = carState; } public String toString(){ return "gear level mode is " + getGearLevel()+ " and speed is : " + getSpeed(); } }
// TestEncapsulation.class public class TestEncapsulation { public static void main(String[] args) { // Car initially in Stop mode AutoGearCar car =new AutoGearCar(AutoGearCar.CarStates.TURNOFF); // Going to set minus speed but thats not possible car.setSpeed(-3); // Going to change gear without start the car car.setGearLevel("D"); // Car turn on by using Key/Switch car.setCarState(AutoGearCar.CarStates.TURNON); // Modes to Drive mode car.setGearLevel("D"); // Speed up to 100 car.setSpeed(100); // Now my car in below state and that state get by getter System.out.println("My Car "+car); } }
Output
Car Speed Error Car gear chenge to drive mode not allowed. because its not started yet My Car gear level mode is D and speed is : 100.0
Abstraction Vs Encapsulation in OOP
Abstraction | Encapsulation |
Encapsulation is an implementation-level programming concept | Encapsulation is everything combining and keeping together as a single unit |
Abstraction is Design level programming concept | Abstract classes or interfaces used to achieve abstraction |
Abstract class or interfaces used to achieve abstraction | Access modifiers used to achieve encapsulation |
Data hiding is part of the abstraction | Data hiding is part of the encapsulation |
Code complexity hides using abstract classes and interfaces | Use getter and setter to hide data |
Advantages of Encapsulation
- Improve the security. Because data is not accessible directly.
- Minimize the Error. Bundle everything internally with appropriate validations
Summary
Encapsulation in OOP (Object-Oriented Programming) is an Implementation level OOP Concept that will be used to improve security by gathering data into a single unit. Getter and setter are used to access data externally because of private modifiers used to attribute hiding.