Encapsulation

Encapsulation

Protecting your properties and methods

Protected properties and methods stems from a word; "Encapsulation" . To become familiar, this article is all about encapsulation.

What is encapsulation?

Encapsulation basically means to express or show the most important facts about something.

As a fundamental aspect of Object Oriented Programming (OOP), it means keeping some properties and methods private inside a class so they are not accessible from outside the class. Some properties and methods are not meant to be accessible to avoid loss of data. Encapsulation is a means to ensure data privacy.

Before diving into how we can make our properties and methods protected, let me define some terms.

  • Internal interface: These are private properties and methods i.e. they are not accessible outside the class.

  • External interface : They are public properties and methods and are accessible outside the class.

Why do we need encapsulation?

  • To prevent code from outside of a class to accidentally manipulate our data inside the class.

  • It makes the application easier to understand.

  • Errors are easily located (debugging) and reduced.

A real life example for illustration.

A machine like a coffee maker can best fit for an illustration. There are lot of components in the machine that are unnecessary for the user to know about. We only get access to other parts that are important. That's what encapsulation is all about.

How to make your properties and methods protected

Creating a class with some properties and a method.

 class mySelf {
    constructor (name, age , gender, password){
        this.name = name
        this.age = age
        this.gender = gender
        this.password = password
    }

    sentence(){
        console.log(`My name is ${this.name} and ${this.age} years old. I'm ${this.gender} and my password is ${this.password}`);
    }
 }
 const omotayo = new mySelf('Omotayo', 21, 'male', 4444)
 omotayo.sentence()  // Returns 'My name is Omotayo and 21 years old. I'm a male and my password is 4444.

All the properties and method are public because we can access them which is very risky. For instance, an intruder might get the data of this user's password which can cause great harm. So the password property needs to be encapsulated. We need it to make it private so that others won't be able to access it.

How we can make it protected

It's by adding an underscore (_) in front of the properties and methods you want to make private .The underscore does not make it truly private but others won't be able override it. It's more like a convention (it was developed by some developers and everyone agrees to use it)

Let's protect the password property

If we want to make it public, we have to create a public method that returns the private property.

class mySelf {
    constructor (name, age , gender, password){
        this.name = name
        this.age = age
        this.gender = gender
        this._password = password
    }
    getpassword(){
        return this._password
    }

    sentence(){
        console.log(`My name is ${this.name} and ${this.age} years old. I'm ${this.gender} and my password is ${this.getpassword()}`);
    }
 }
 const omotayo = new mySelf('Omotayo', 21, 'male', 4444)
 omotayo.sentence()  // Returns 'My name is Omotayo and 21 years old. I'm a male and my password is 4444.

That's it. There are other ways of making your methods and properties fully private. This is just a basic way of doing it.

Conclusion

I'm going to drop another article on how to make it fully private. Like, comment and share this article. It's a way of supporting me. Stay tuned for the next article.❤️

Did you find this article valuable?

Support Charismatic Omotayo's Blog| Mustafa Umar by becoming a sponsor. Any amount is appreciated!