Site icon ConnectStudy

Access Control In Programming Languages

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:

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.

Exit mobile version
Skip to toolbar