Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Can anyone please explain access-control in swift3 to me?

So I've been learning swift for a while now. As I've started to assimilate the object oriented programming paradigm, I've become perplexed with the concept of access control. I've searched an searched (and read the documentation), but only seem to come across advanced use cases with unfamiliar jargon and OOP principles which I don't fully understand.

Anybody care to provide a use case for each access modifier (private final, private, public, etc.)?

Thanks in advanced. -Charles

1 Answer

Great question!

To begin it looks like there's a bit of overlap in your question between access-control and inheritance. These two topics have a lot to do with each other so it's easy to get them confused.

In your question above you mention the final modifier which is used to prevent inheritance or otherwise prevent that method, property, subscript, or class from being overridden. This can easily be seen as controlling access of a class but is technically different because you're not trying to hide implementation of these things like you would by using the private modifier in front of a class declaration.

A class marked as final can still be private, public or any of the other access modifiers as you outlined above in your question by noticing that you can have private final in front of the class declaration. That just means no one can inherit the class and no one will be able to see that class inside other's to use which is a little redundant but necessary to insure it's not used again.

Okay so to get to the meat of your question about why and when to use private, public, etc...

Using access-control is exactly what it sounds like in that you're controlling how people interact with parts of your code so that begs the question of why you'd do that. It's because the inner workings of your classes aren't always parts you want people to be able to modify. Hence the use a private access modifier.

Private is the most restrictive modifier and will limit all use of that method property, etc to that class and can never be seen or used outside of it. So if you have a really vital method you're calling inside your class and you need it to work a certain way every time you don't want other people calling it outside the class so you'd mark it as private.

The public/open modifier is the one you'd use to open up visibility to anyone who uses your class. These allow people to manipulate your class and are the parts you'd like to expose for use. If you can help it I'd only use this for methods, variables, classes, etc that are specifically for use and won't cause problems in your class if they're changed.

The internal modifier is used to make sure no classes outside of your project can use whatever you've set as internal. For instance you might have some helper methods that are called throughout a couple classes so you abstracted those out to a helper class maybe and set it to internal. They're vital to a few classes but not vital to another person's project if they wanna use your framework so you'd set it to internal.

You have further control with file-private because that will limit the access from the project to the internal file that the thing you've defined as file-private is located in.

To be honest unless you're creating frameworks and open sourcing them on Github or some other distributed system I'd only be concerned with public and private. These will be all you'll need inside a normal project.

My other recommendation is to set everything to private instead of public as a default and if you find yourself needing something to be public i'd just go to that class and change it to public. This is a great practice to get into so you've protected and hidden as much of your class as possible from other classes.

I hope this has been helpful! If you have anymore questions feel free to ask!

Cheers :)

Josh,

Thank you so much for clearing that up for me. That confusion between inheritance and access-control was exactly what made the reading that page of the documentation such a daunting task. I guess, I sort-of dosed off when learning that part of inheritance so seeing all of those modifiers in conjunction without any explanation was very off-putting. In any case, thanks for going that in-depth.

-Charles