What Are Access Modifiers?

What Are Access Modifiers?

ยท

3 min read

There's this really cool feature on a lot of social media platforms where you can choose who has access to your posts; it's kind of a form of public privacy in the sense that you're able to expose things to the internet yet control how much of the internet is able to perceive your content. It feels like a patent, though it's not ๐Ÿ˜….

In software development, we can't give what we don't have and you find that a lot of cool features have some ideological similarities with the status quo of software development. Today we'll be looking at access modifiers as they relate to software development.

A man showing a little girl images on a laptop

A relatable explanation

Access modifiers exist as one of the enablers of object-oriented programming (which is a paradigm of software development just like functional programming is). OOP (Object Oriented Programming) depends heavily on breaking code into modular pieces examples of which are "class" and "method", we do however need to declare the scope of their reference.

Popular access modifiers include; public, private, protected, internal, protected internal, private protected. Yeah, I know it's a lot to take in, but we're basically talking about backend software development ๐Ÿ˜. This isn't going to be as shiny as frontend development, which is why you need to appreciate backend developers for what we do ๐Ÿ˜‚.

cat's eyes in the dark

Taking a Look at The Access Modifiers

We'll start with public; public access modifiers are assigned to code that can be referenced from anywhere within the solution and even out of it. By reference, we mean other bits of code can leverage the utility of a block of code that has a "public" access modifier.

Public access modifier is easily the most popular access modifier. If you recall, I used it last Friday when we looked at functions. Public is just convenient to use as a default access modifier for beginners. There are, however, security concerns that may be raised with having blocks of code be too accessible. Averagely, a public access modifier is enough.

Private access modifiers are used to ensure that members of a class can only be leveraged by code in that same class. This is the equivalent of "whatever happens in Vegas stays in Vegas." ๐Ÿ˜‚.

Protected access modifier is similar to private, only that with protected, any class that inherits from the parent class can access the members of the parent class. Think of this as how children, in reality, take their parent's stuff despite having their own stuff ๐Ÿ˜‰.

Internal access modifier is a step below public, internal exposes blocks of code to other blocks of code as long as they're within the same compiled solution. It's kind of like how you have access to more opportunities within a country as long as you're their citizen ๐Ÿ˜.

Protected internal is a mixture of protected and internal (literally), code within a compiled solution and code that inherits from a class within a compiled solution can leverage the utility of the "protected internal" code, in reality, it's just like the EU or trade block ๐Ÿค‘.

Private protected simply refers to blocks of code that allow its derivatives within the same compiled solution.

Picture of a girl walking with balloons

Finally

Access modifiers are one of the key features that make up the OOP software development process, a lot of the internet and other digital infrastructure depends on these abstractions. While it may seem like a lot to take in, over time it starts to make sense. Inheritance has a huge effect on what goes on in backend software development.