What is Encapsulation in Object-Oriented Programming? Great 1 of OOP

Encapsulation in Object-Oriented Programming
Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on whatsapp
Share on pinterest
Share on print

Encapsulation in 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, example of encapsulation in java, advantage 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 paddle, steering wheel, and gear changing handle 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.

Encapsulation
Encapsulation

According to the example, we can see everything bundled into a single unit with user access points. All validation doing 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 possible 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 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

Abstraction Encapsulation
The concept of the abstraction is showing the required information to the outside and hiding the lower level complex implementationEncapsulation is everything combining and keeping together as a single unit
Abstraction is Design level programming conceptEncapsulation is an implementation level programming concept
Abstract class or interfaces used to achieve abstractionAccess modifiers used to achieve encapsulation
Data hiding is part of the abstractionData hiding is part of the encapsulation
Code complexity hides using abstract classes and interfacesUse getter and setter to hide data
Key Identifiers of Abstraction and Encapsulation

Advantages of Encapsulation

  • Improve the security. Because data is not accessible directly.
  • Minimize the Error. Bundle everything internally with appropriate validations

Summary

Encapsulation in Object-Oriented Programming is an Implementation level OOP Concept that will use 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.

Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on whatsapp
Share on pinterest
Share on print

7 thoughts on “What is Encapsulation in Object-Oriented Programming? Great 1 of OOP”

  1. I do accept as true with all the ideas you have introduced to your post. They are very convincing and will certainly work. Nonetheless, the posts are too quick for starters. May you please lengthen them a little from subsequent time? Thanks for the post.

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Articles
You May Like
Subscribe to our Newsletter
Scroll to Top