JavaScript Lesson 27 – Encapsulation in OOP

Tram Ho

Following Inheritance , Encapsulation is always concerned and supported first and foremost compared to Abstraction and Polymorphism . To briefly define, Encapsulation can be understood simply as allowing to hide the local property and method elements of an object . This will help ensure that code interacting from outside of the object won’t be able to directly use or make changes to local elements.

For complete OOP languages ​​like Java or C# , the Encapsulation feature is provided by a set of keywords called Access Modifier – roughly understood as labels that specify access rights to the object ‘s elements. Central Java provided keywords include:

  • public – always available with reference code written anywhere.
  • private – only available with reference code defined within that class ; Referenced code written in inherited class also cannot access private elements in the parent class .
  • default – available with reference code defined in the same package .
  • protected – available with reference code defined in the same package and inherited class outside of that package .

Public & Private

The public keyword is obviously nothing worth noting, so we’ll just do an example with private . In the example code in the previous post, we can tweak it a bit and use private labels for the name and age attributes. Now in the main program we will try to access these properties directly.

And as a result, when we click Run to run the test, we will see an error message that name and age are labeled private , so they are not available in the statement accessing from outside the class Entity in the main program.

Now we’re going to modify the main program a bit and instead of accessing age directly, we’ll call the whisper method. This method is inherited by the class Universe class Entity , and because whisper is defined in the class Entity , it is possible to access the private age defined in the class Entity .

We already know that in JavaScript , the mechanism of public and private operations as mentioned above is also expressed in the same way. By default, all property and method elements are implicitly public by JavaScript ; And Java ‘s private keyword is denoted by JavaScript with a # attached in front of the identifier names of the elements we want to hide.

Default & Protected

The two keywords default and protected are designed by Java with functionality related to a concept called package . A package in Java is simply understood as a directory containing interrelated code files. And if a program is written in Java with many code files grouped in different directories, then the default and protected keywords will represent directory-level packaging functionality, also known as package level .

In the JavaScript environment we don’t have the same concept of encapsulation at the directory level. Therefore, we will only be interested in a single feature of the protected keyword in Java that we can borrow the operation logic into JavaScript code. That is, elements labeled protected are not available everywhere like public , but also unlike private , which are only available in the class it is declaring directly.

Outside of the scope of the class being defined directly, protected elements are also available to access code defined in derived class .

Now we will try to modify the above example code and set protected labels for the name and age attributes. Then, in the definition of the class Universe , we will create an echo() method and access it through the this pointer.

As we have seen, name and age are defined as protected in the class Entity , and are available in the reference code defined in the class Universe .

To clarify the difference between protected and private , you can try converting the labels of name and age back to private . Thus, the test runner will report an error because the echo() method defined in the class Universe will not be allowed to access the private elements of the class Entity .

As for clarifying the difference between protected and public , due to the limitation of the Online REPL tool in use, we cannot create a code file in another package to try to refer to protected elements. However, as mentioned earlier, if the code is written in a file different from the directory with the class Universe and does not inherit the class Universe , it will certainly not be able to access the protected elements.

Protected in JavaScript

Thus, there is a part of the logic of the protected label’s operation that can be useful in some cases that we might want in JavaScript . It is the ability to encapsulate in the same line of inheritance from the place of the class that directly defines protected elements.

Reference code written in the class itself or derived class can use references . to access protected elements. Otherwise, logical code defined elsewhere in the program will not be able to directly access protected elements.

At the moment, JavaScript does not support this feature. However, quite a few programmers think that protected is important, so they have created a naming convention with the _ symbol in front for property and method elements that they will never access directly from the specified code. meaning outside the line of inheritance.

That is, elements with names like _name , _age , etc. are technically still public and accessible by the defined code anywhere. However, when coders make the convention that they will only make references to these elements when coding the definitions of class in the same line of inheritance, the _ elements will obviously behave the same way. with protected .

Reference source: Private & Protected _by JavaScript.info

Well.. actually, the naming convention for protected elements begins with the _ symbol, borrowed from Python . There, on the technical side, the opening elements _ actually have the ability to hide from the code referencing from outside the legacy line. Therefore, coders will also be able to receive operational logic error messages if they mistakenly write referenced code from a location that does not have access.

[JavaScript] Lesson 28: Polymorphism in OOP

Share the news now

Source : Viblo