Mark As Completed Discussion

Controlling Access to Members of a Class

The first rule of OOP is Abstraction. To make an abstract class, we need a methodology to maintain access to the members of a class. Many OOP-supported languages have special keywords for these methodologies called access level modifiers. Oracle has a very self-explanatory definition for these modifiers:

Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:

  • At the top-level—public or package-private (no explicit modifier).
  • At the member level—public, private, protected, or package-private (no explicit modifier).

Controlling Access to Members of a Class

A class is defined by a starting optional modifier. Additionally, each attribute and method within that class will also need its own access modifier.

1class [Name] {
2    [property name];  // No type in JS
3
4    [method name]() {
5        // Implementation
6        return [Return type R-value];
7    }
8}

What are these keywords? Let us go through them one by one.