Nowadays, technologies have taken over the entire world by becoming our greatest tools. The politics surrounding our lives have also began to lean towards a fundamental value of human rights: Privacy. When developers produce software, they must be attentive to protect the privacy of the users by enforcing the security of the program. Luckily, in major programming languages, we are gifted with Access Control.

A lot of people began programming by learning either Java, so we are going to start there as well. If you have programmed using Java before, you should probably recognize this:

public static void main(String [ ] args) {
    
}

It is the main method of every Java program, the beginning of all Java applications. Even the most basic program has access control automatically configured, and that is the very first keyword: public. Different programming languages has slightly different implementations for access control, but their functionalities are identical. According to Swift.org:

Access control restricts access to parts of your code from code in other source files and modules. This feature enables you to hide the implementation details of your code, and to specify a preferred interface through which that code can be accessed and used.

Swift Language Guide

So What Are the Access Modifier Keywords?

Access Modifiers in Java

Access Levels in Swift

This is taken directly from the Swift Documentation:

  • Open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework. The difference between open and public access is described below.
  • Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.
  • File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.
  • Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.

By default, Swift uses internal if no level is specified.

Some Examples of Access Control

Please keep in mind that different languages have different behaviors when it comes down to access level inheritance. If you do not know what if means, there is an example below:

public class SomePublicClass {                  // explicitly public class
    public var somePublicProperty = 0            // explicitly public class member
    var someInternalProperty = 0                 // implicitly internal class member
    fileprivate func someFilePrivateMethod() {}  // explicitly file-private class member
    private func somePrivateMethod() {}          // explicitly private class member
}

public enum CompassPoint {
    case north // implicitly public
    case south // implicitly public
    case east // implicitly public
    case west // implicitly public
}

As you can see in Swift, entities within a class does not inherit its public behavior, while retaining internal implicitly. In cases such as enum (pun intended), the enumeration cases do inherit CompassPoint‘s public modifier.

Another important detail to remember is that when you are subclassing, the subclass cannot have a higher access level (less restriction) than the hyperclass.

This should cover most of the necessary details for you to understand Access Control in programming languages. If you have a question or want to learn more in depth, you may comment below or checkout my profile to schedule a session with me. Please keep in mind that I am very inexperienced in writing articles like this so please utilize the comment section to suggest fixes for any mistakes I’ve made in this post.