Encapsulation is really one of the foundational aspects not only of Object-Oriented Programming, but also of classes and structures in general. In this lesson we will cover the finer points of this concept.
1.Introduction4 lessons, 26:24
2.Language Constructs12 lessons, 1:51:34
3.Swift and Object Oriented Programming5 lessons, 27:52
4.Built-In Types5 lessons, 42:18
5.Conclusion1 lesson, 01:19
In this lesson, we're going to cover the topic of encapsulation and how it pertains to object oriented programming through the swift language. Now, encapsulation is another interesting topic, when it comes too OOP, in the fact that, what it allows you to do is to hide some of the complexities of your application, or of your objects from the end user, so that they don't need to be concerned with those things, all they need to know are the readily available pieces of functionality and properties or methods, within a class or a structure and how to use those that are publicly exposed. Now, I just used a word there that is going to be interesting to most, and that, I said the word public, now public assumes that there are access modifiers within the Swift programming language, and by that I'm talking about things like public, private, and internal. The basic concepts behind these is that if you use the public access modifier on a property or on a method, that means it's going to be available to everyone that has access to this particular item. And we're going to be talking about this within the context of classes here, so if I have a public property or a public method within my class, then everybody that has access to the class in question is gonna have access to those pieces of functionality within the class. Now, private is a little bit different than you may imagine, in some programming languages the private access modifier is going to state that nobody outside of the actual implementation of the class is going to have access to this particular piece of functionality, whether it's a property or a method. But within Swift it's a little bit different, private is going to mean that no one outside of this particular file is gonna have access to these pieces of functionality. So if I create two different classes within the same file within Swift, and I have private properties or methods within those classes, I'm gonna have access to those as long as I'm in the same file. Everybody outside of that file that's gonna access those particular classes are not gonna have access to it, so that's a little bit of a, of a different concept, depending on what language you're coming from. The internal access modifier is actually the one that is used by default and according to Apple's documentation, the 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. So we're talking about things like frameworks or any sort of maybe namespace concepts where you're defining this particular property or method within a class are only gonna be accessible within that structure. So basically, we're going to use these things to limit, who can access them and when they can access them. So let's go ahead and take a look at how this is going to work within Swift. So when we're working with a concept like encapsulation, one of the more common ways that you're going to see this represented, is by creating some functionality within a class or within a structure,that is going to expose publicly some form of properties or methods that you want the end user or whoever the consumer of this class is, to use. But then there's going to be a number of other things within there, that you don't necessarily want them to concern themselves with, as it's more of just an implementation detail that they shouldn't really care about. So in a previous example I started to define something called a Car, which is just going to be a class and then I'm going to put in here a number of properties. So, this is before I said, okay, we have a number of wheels and we defined that to be in integer and it started off with four, and we had a number of doors and that is also going to be an integer set to four. And then we started to define some methods, some functionality within our class, in order to interact with it, so one of them that we created is something called Drive. Now let's say that we have this, this method is going to be accessible and we want them to have accessibility to this everywhere outside of this particular file, and really anybody that's going to use this. So, by default, like I said, this stuff is going to be internal, so it's only gonna be accessible within its, it containing module. So, I already don't like that, I want this to be accessible to everybody, so I'm going to decorate this class, with a public keyword. So, that's gonna be fine, that's not gonna be a problem. But, now when we start to define some other pieces of functionality in here, I want this drive method to also be public, I also want these properties to be public. So I want all this stuff to be accessible to anyone that's using this particular class regardless of where they are within the application, or within any of the Containing modules or outside of them. But, let's say now that this drive method is pretty simple, they just call, they create a new class object and they call the drive method, but there's some complexities to that drive method, that I don't want them to be worried about. So let's say, that in order to drive, I need to be able to start the car, so let's say start Car, our start is going to be a method, so this is going to do something as well and then we're going to have to maybe change gears, or maybe we're gonna have to put into drive or something along those lines, so we're gonna have to do some other operations within that drive. But we don't do the end user to really care about that, so what we would do, is we would change the access modifiers to these to maybe be private. Like such and then from within our Drive method, we would execute the Start method and then we could put it into drive and things like that. So this would be the concept of encapsulation, where the end user doesn't necessarily need to know about these particular functions or these methods. It's only going to be accessed indirectly through the call to this drive method. And I could do this for priv, properties and for methods as well, so I could put another property in here, I could say private var gas mileage or, or something along those lines or I could then change this to be something else. So this is going to allow us to then create these, very rich objects that can be very simple from the outside world perspective, but then ultimately the data and the operations that are going on within the object or within the class can be very, very complex, but as far as everybody else in the outside world is concerned it's really not that hard, you have a drive method and you have something else that are publicly exposed, and that's all you really ultimately care about. So there you have it, that's the basic concept behind encapsulation and if it's an easier way for you to think about it, you can just think about it with the idea of, you're hiding information from the end user, don't show them what they don't need to know, only provide to them the important aspects of the class or of the implementation