DEV Community

Cover image for Unveiling the Magic of Access Modifiers in OOP! πŸ”’
Dominic Azuka
Dominic Azuka

Posted on

Unveiling the Magic of Access Modifiers in OOP! πŸ”’

Access Modifiers in OOP control the visibility of class members. There are four types: public, protected, private, and internal.

1. Public Access Modifier:

Image description

2. Private Access Modifier:

Image description

3. Protected Access Modifier:

Image description

4. Internal Access Modifier:

Image description

Client-side Example: ReactJS
In React, you can control component accessibility using JavaScript's export keyword.

Image description

Server-side Example: Node.js + Express:
In Express, access control can be managed in route handlers.

Image description

Database Example: MongoDB
MongoDB allows you to define field access within documents.

Image description

Best Practices

  • Use the least restrictive access modifier needed.
  • Encapsulate internal details to avoid unintended access.
  • Consider security when exposing sensitive data.
  • Maintain consistent access control across layers of your application.

Top comments (0)