DEV Community

Cover image for What is Encapsulation
mahdi
mahdi

Posted on

What is Encapsulation

Summary:

Encapsulation is a fundamental concept in object-oriented programming that involves restricting direct access to certain components of an object. By encapsulating data and methods within a class, encapsulation ensures that the internal state of an object is accessible only through a public interface, typically consisting of the object's methods.

better understand

img

Why encapsulation is important

  • Data Protection: Encapsulation allows for the hiding and protection of an object's internal state, preventing direct manipulation by external entities.
  • Controlled Access: With encapsulation, access to an object's data is controlled through methods, enabling validation and manipulation of data before it is accessed or modified.
  • Enhanced Security: By restricting access to sensitive data, encapsulation enhances security and reduces the risk of unintended data corruption or manipulation.

Notably, languages like JavaScript, akin to C++, Java, and C#, do not inherently enforce encapsulation. Instead, developers often adopt conventions, such as prefixing private properties with an underscore (_), to signify intended privacy.

class User {
  constructor(name, age) {
    this._name = name; // Conventionally marked as private
    this._age = age; // Conventionally marked as private
  }
  // Getter method for name
  get name() {
    return this._name;
  }

  // Getter method for age
  get age() {
    return this._age;
  }

  // Method to update name
  updateName(newName) {
    this._name = newName;
    console.log(`Name updated to: ${newName}`);
  }

  // Method to update age
  updateAge(newAge) {
    this._age = newAge;
    console.log(`Age updated to: ${newAge}`);
  }
}
// Creating an instance of the User class
const myUser = new User("Ali", 19);

// Although JavaScript doesn't strictly enforce encapsulation, it's customary
// to adhere to conventions like prefixing private properties with an underscore (_).

// Accessing account details through getter methods
console.log("Name:", myUser.name); // Outputs: Ali
console.log("Age:", myUser.age); // Outputs: 19

// Updating name and age through methods
myUser.updateName("Mahdi"); // Outputs: Name updated to: Mahdi
myUser.updateAge(20); // Outputs: Age updated to: 20

// While JavaScript doesn't mandate encapsulation, it's recommended to utilize
// getter methods to access properties. Direct manipulation of underscore-prefixed
// properties should be avoided for better code maintainability and encapsulation.
console.log("Name:", myUser.name); // Outputs: Mahdi
console.log("Age:", myUser.age); // Outputs: 20
Enter fullscreen mode Exit fullscreen mode

Languages that use encapsulation:

Encapsulation is a key concept in many object-oriented programming languages. Some of the languages that enforce encapsulation include:

  • Java: Encapsulation is a fundamental principle in Java. It is achieved through the use of access modifiers such as private, protected, and public, which control the visibility of class members.
  • C++: Encapsulation is supported in C++ through access specifiers like private, protected, and public. These specifiers determine the accessibility of class members.
  • C#: Similar to Java, C# provides access modifiers to enforce encapsulation, allowing developers to control the visibility of class members.

Encapsulation Example in Java:

public class User {
    private String name;
    private int age;

    // Constructor
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter method for name
    public String getName() {
        return name;
    }

    // Getter method for age
    public int getAge() {
        return age;
    }

    // Method to update name
    public void updateName(String newName) {
        this.name = newName;
        System.out.println("Name updated to: " + newName);
    }

    // Method to update age
    public void updateAge(int newAge) {
        this.age = newAge;
        System.out.println("Age updated to: " + newAge);
    }

    // Main method
    public static void main(String[] args) {
        // Creating an instance of the User class
        User myUser = new User("Ali", 19);

        // Accessing account details through getter methods
        System.out.println("Name: " + myUser.getName()); // Outputs: Ali
        System.out.println("Age: " + myUser.getAge()); // Outputs: 19

        // Updating name and age through methods
        myUser.updateName("Mahdi"); // Outputs: Name updated to: Mahdi
        myUser.updateAge(20); // Outputs: Age updated to: 20

        // Accessing updated details through getter methods
        System.out.println("Name: " + myUser.getName()); // Outputs: Mahdi
        System.out.println("Age: " + myUser.getAge()); // Outputs: 20
    }
}
Enter fullscreen mode Exit fullscreen mode

Comparison with Other Paradigms:

Object-Oriented Programming (OOP):

In OOP, encapsulation is a fundamental principle that promotes data hiding and abstraction. Classes encapsulate data and behavior, exposing only a well-defined interface to interact with objects. Access modifiers like private, protected, and public regulate the visibility of class members, ensuring proper encapsulation and encapsulation.

Functional Programming (FP):

In functional programming, data encapsulation is achieved through immutability and pure functions. Instead of encapsulating state within objects, FP emphasizes the use of functions that operate on immutable data structures. Encapsulation is achieved by limiting side effects and ensuring that functions do not modify external state, enhancing code predictability and maintainability.

Comparison:

  • Encapsulation in OOP: In OOP, encapsulation is achieved through classes, which bundle data and behavior together. Access modifiers control the visibility of class members, enabling encapsulation and information hiding.
  • Encapsulation in FP: In FP, encapsulation is achieved through immutability and pure functions. Data is encapsulated within immutable data structures, and functions operate on these structures without modifying them, ensuring encapsulation and referential transparency.

Advantages:

  • OOP: Encapsulation in OOP promotes code reusability, modularity, and maintainability by encapsulating data and behavior within classes. Access modifiers provide fine-grained control over visibility, enhancing encapsulation and information hiding.
  • FP: Encapsulation in FP promotes code purity, predictability, and concurrency by emphasizing immutability and pure functions. Immutable data structures ensure thread safety and prevent unintended side effects, facilitating encapsulation and referential transparency.

Best Practices:

  • OOP: In OOP, adhere to access modifiers to enforce encapsulation and information hiding. Use private members for internal implementation details and provide public interfaces for interaction with objects.
  • FP: In FP, prefer immutable data structures and pure functions to achieve encapsulation and referential transparency. Avoid mutating external state and favor function composition for building complex systems.

In conclusion, while both OOP and FP embrace encapsulation, they employ different mechanisms and paradigms to achieve it. Understanding the nuances of encapsulation in these paradigms enables developers to write more robust, maintainable, and scalable code.

Example(FP):

function createUser(name, age) {
  return Object.freeze({
    getName() {
      return name;
    },
    getAge() {
      return age;
    },
    updateName(newName) {
      // Return a new object with updated name, preserving immutability
      return createUser(newName, age);
    },
    updateAge(newAge) {
      // Return a new object with updated age, preserving immutability
      return createUser(name, newAge);
    },
  });
}

const myUser = createUser("Ali", 19);

// Accessing account details through getter functions
console.log("Name:", myUser.getName()); // Outputs: Ali
console.log("Age:", myUser.getAge()); // Outputs: 19

// Updating name and age through methods
const updatedUser = myUser.updateName("Mahdi"); // Outputs: No console log (pure function)
const finalUser = updatedUser.updateAge(20); // Outputs: No console log (pure function)

// Accessing updated details through getter functions
console.log("Name:", finalUser.getName()); // Outputs: Mahdi
console.log("Age:", finalUser.getAge()); // Outputs: 20

Enter fullscreen mode Exit fullscreen mode

Conclusion

Encapsulation is a fundamental principle in object-oriented programming that ensures the integrity and security of data within an object. By encapsulating data and methods, encapsulation restricts direct access to an object's internal state, promoting data protection, controlled access, and enhanced security.

Encapsulation plays a crucial role in maintaining code quality, facilitating code reusability, modularity, and maintainability. It enables developers to build robust and secure applications by protecting sensitive data and controlling access to it.

In conclusion, understanding and implementing encapsulation are essential skills for developers, enabling them to write clean, secure, and maintainable code. Whether working in object-oriented or functional programming paradigms, embracing encapsulation principles leads to better software design and development practices.

Top comments (2)

Collapse
 
kooiinc profile image
KooiInc • Edited

Your FP example is neither pure nor immutable. console.log is considered a side effect, and the created Object from createUser (factory function) mutates it's state.

This would be more in line of your reasoning imho:

function createUser(name, age) {
  return Object.freeze({
    age: age > 0 ? age : `not set`,
    name,
    updateName(newName) { return createUser(newName, age); },
    updateAge(newAge) { return createUser(name, newAge); },
  });
}
Enter fullscreen mode Exit fullscreen mode

Here name and age are encapsulated by closure and an update does not mutate the original but delivers a new Object (like the immutability of Strings).

See this small StackBlitz demo

Collapse
 
m__mdy__m profile image
mahdi

Yes, I saw it. Thank you for your notice
I would never have noticed this mistake
I fixed it