Encapsulation in java

The dictionary meaning of Encapsulation is “The condition of being enclosed.”

Capsule:

A capsule refers to a small container that contains a dose of medicine.

Definition of Encapsulation:

Encapsulation is a way of wrapping code and data into a single unit.

Encapsulation in the real world:

Let us take an example of HR in a company. We communicate through HR and not directly with the departments. HR acts as a public interface here.

Encapsulation in programming:

Encapsulation is a way of declaring the data members as private and accessing the data members through public methods (getter and setter methods). A private field in Java can’t be accessed outside the class which means data is hiding within the class. Because of this reason, encapsulation is also known as data hiding.

Important points:

  1. The Main Concept behind encapsulation is ‘control over the data’. It is achieved using class and access modifiers private, protected, and public. Class acts as a container that contains code and data.
  2. Factory pattern and Singleton pattern in Java are based on the concept encapsulation.

Example:

Car.java

package com.w3schools;

public class Car {	
    //Data members
    String carColor; 
    int carSpeed;
    
    //Getter Setter
    public String getCarColor() {
        return carColor;
    }
    public void setCarColor(String carColor) {
        this.carColor = carColor;
    }
    public int getCarSpeed() {
        return carSpeed;
    }
    public void setCarSpeed(int carSpeed) {
        this.carSpeed = carSpeed;
    } 
    
    void addCarDetails(String color, int speed){
        carColor = color; 
        carSpeed = speed;
    } 
    
    void displayCarDetails(){ 
        System.out.println("Color: " + carColor); 
        System.out.println("Speed: " + carSpeed); 
    } 
    
}

CarTest.java

package com.w3schools;

public class CarTest {
    public static void main(String args[]){ 
        //Creating Car Objects 
        Car car1 = new Car(); 
        Car car2 = new Car(); 
        
        //Need object here because method is non-static. 
        car1.addCarDetails("Blue", 180); 
        car2.addCarDetails("White", 130);
        
        System.out.println("Car1 Details: ");
        car1.displayCarDetails(); 
        
        System.out.println("Car2 Details: ");
        car2.displayCarDetails(); 
        
        //Update Car 1 color using setter method
        car1.setCarColor("Red");
        
        //Print Car 1 updated details
        System.out.println("Car1 Updated Details: ");
        car1.displayCarDetails();
        
        //Print Car 2 speed using getter method
        System.out.println("Car2 Speed: " + car2.getCarSpeed());
        
    }
}

Output

Car1 Details: 
Color: Blue
Speed: 180
Car2 Details: 
Color: White
Speed: 130
Car1 Updated Details: 
Color: Red
Speed: 180
Car2 Speed: 130

Advantages/Benefits of Encapsulation

  1. An immutable (read-only or write-only) class can be made.
  2. Provides control over the data.
  3. It helps to achieve high cohesion and low coupling in the code.
Please follow and like us:
Content Protection by DMCA.com