02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Encapsulation

What is Encapsulation? the idea of encapsulation is really easy way easier then actually understanding what that word even means. Encapsulation means to enclose or to cover , in a small container like a capsule. In this video we explore what that means and see it in action in a visual demonstration.

Video Transcript

00:00All right, so encapsulation. Before we dig too deep in it, one thing is, which is really annoying
00:04with programming languages in general, usually the terms are, the scarier the term is, the more
00:10simple the idea is. I have no idea why it's that way, but I always was intimidated by the big
00:16words until I realized that, you know, there's just big words. So encapsulation, in a nutshell,
00:21the whole idea of encapsulation is very, very, very simple. Now we already are building classes
00:28and some classes have a lot of functionality just because they need a lot of functionality.
00:32So first of all, let's just look at the definition of encapsulation. Encapsulation, the non-programmer
00:37term, I'm not even looking at the programmer term because it's not really important. Encapsulation,
00:41to enclose or to cover in a small container like a capsule. And really, that's really
00:47all encapsulation is. Now when you go down to how do you actually do it, all right, so there's
00:53a few technicalities involved, but you first have to get that whole concept. The concept is,
00:58we have an API, we already learned what an API is. So let's say we have your phone, phones today
01:03standardly could text, they could call, they could get calls. Alas, we have here a new type
01:10of element, we have a payphone. Now a payphone doesn't have any text at all. It can only call
01:16and it could get calls, but there's a lot of limitations and processes involved, especially today
01:20when a payphone, well, they don't really exist anymore. But imagine that they still do. And the
01:25payphone basically needs to make sure that you're a valid person that you paid and has to add all
01:30these extra stuff to a specific phone. Now, don't forget that our text call and get calls are all
01:36public. So if we had a payphone and it extended the phone, then automatically the payphone would
01:42have access to text calls and get calls automatically. And then it would create some
01:46issues and we have to build a really complicated application to block the text, to block the calls,
01:51to block the list, to block the that. Or we could in a different way, instead of,
01:57instead of extending our payphone from a phone, we could create a payphone that's a standalone
02:02object doesn't extend anything. And inside of that payphone, we create a new phone,
02:08a hidden phone that's inside of our payphone, it's private, no one can access it, but that
02:12phone. Now that we created a phone that's a completely private phone, we could create
02:17a new function called calls. We have a new function called get call that has no connection
02:21to the phone one. And then we could charge a user for a payment when the user pays, then we'll
02:26internally call the phone itself, we'll internally get calls. The idea itself of
02:32encapsulation is basically basically taking something bigger, hiding it all this dotted
02:37lines is hiding it inside of my class. This is private stuff that belongs to this new class.
02:42It's an object that's sitting inside of our class. And now that I have that object inside of my
02:46class, I could then expose whatever I want to expose. Now I want to see this in action, just
02:51for a second, then I'll make sure that you guys actually get that encapsulation idea.
02:55So if I go into a class, and imagine that we have our phone class. And in our scenario,
02:59we're going to have here our payphone. Now instead of making a payphone that extends
03:04that specific phone, so instead of a payphone that extends phone, that we don't want to do
03:09that, that just makes our life complicated. How about instead of that we create, as we said,
03:13we create a private variable. And don't worry about the syntax, if you're not an object
03:19oriented program, or just jump over to our site, or catch an op book, the idea is really just to
03:24get the idea. So, you know, deliberately, please don't over analyze the text, focus on the idea,
03:29because that's going to make your life easier as you get into these stuff, if you haven't done
03:33it already. Alright, and we're going to create a phone here, which is going to be a type of
03:37phone. And as soon as our payphone is, its constructor is called, as soon as our code
03:43initiates, I'm just going to automatically create a new phone. Now our new phone has two
03:49functions, imaginary functions right now, because this is all theoretical. And one of the functions
03:54was call. All right. Now, this is a theoretically not letting us call, but this is just a
04:02theoretical, not real code. Okay, so we're going to do that for our theoretical example.
04:08Now, in our call, we can have your all these different question marks and checks and make
04:11sure that the user paid, and if the user did pay, then we could call the phone and make that call
04:16and done now we only created. And you know, just because I hate these type of blue, call me,
04:24let's say, right, so we don't get that blue thing. What we get out of this is even though
04:29our phone had a lot of different functions, and maybe properties, we're not exposed to them. So
04:36when a user goes to the payphone, all I really have access to is what we want them to have access to.
04:41And what I just said now in five minutes, is exactly what capsulation encapsulation is. So
04:47when we look at that definition, and I'm ignoring the programmer definition, because it's just way
04:52more complicated than that. But that's all it really means is to enclose, or to cover in a
04:58small container like a capsule. And that's really all encapsulation is. Now, one more
05:03thing I want to say about this before we, I give you the stage to see if you have any
05:07questions about that or any further questions, is now that we saw the idea of encapsulation,
05:14imagine a world where you don't have to imagine, because now if you want to do it, imagine that
05:20there's a futuristic phone that's also a phone and also a video phone. But our data type of
05:25phone doesn't really have video. And we have another class that's a video class, we could use
05:31both of them. And instead of having a payphone that actually has real substance, we could have
05:37a sorry, a video phone, that behind the scenes is just two different classes that are working
05:42with each other, such as the video and the phone. And we're encapsulating basically we're taking,
05:48we're basically merging together a few different different classes to create whatever we need.
05:53Okay, so that's just throwing into the air an idea of basically how you take this and how you
05:58take it and move it forward. And really the idea is, is, you know, you take five, six different
06:03classes that might be complicated, and then you create one class that's really, really simple,
06:07because your application only needs to do one or two or three or four different things,
06:11you only expose those three, four stuff in that class. And now anytime you're interacting with
06:15those 10 really complicated classes, you don't even know that you're doing that. Because all
06:20you're doing is interacting with a really simple class that you created and defined,
06:23because you knew that you had a few specific stuff you wanted to do with it. Okay, so I'm
06:27going to take questions now make sure that everyone's with me. Everyone's got an encapsulation. And
06:32obviously, when you get down to the nitty gritty and actually do it, you might have issues,
06:36but the idea itself, if you get that idea, all the rest is going to sit in place.
06:45Yeah, exactly. But not only that it's composed, it's composed, which is a really good name for
06:51it as well. Basically encapsulation is basically we're taking something that has a lot more
06:55functionality, and we're hiding that functionality inside of a new class. But yeah, it's a
07:00composing because we could take a lot of composition would basically be taking a few classes,
07:05we could take five, six different classes, such as let's say a video phone, and that video phone has
07:10internally, it's not really a phone. Internally, it's just an empty class that has an instance
07:15of a phone in it, and an instance of a video in it. And with those two classes,
07:20it then creates an interface that enables users to also call and also have video channels.
07:25Okay, so that's exactly exactly true. So composition is kind of an even more sophisticated
07:32encapsulation, it's an encapsulation that encapsulates more than one class, I would say.
07:38Okay, but the naming is not as important as the concept and the idea of sometimes you want to
07:42have an inheritance. And sometimes you want to hide things inside of a class and basically
07:47create a new interface for your new class. A good example of something like that also in
07:53an encapsulation example, for example, and this is something that's actually related to a topic
07:57that we'll talk about really briefly in a few minutes, is imagine a scenario where you built
08:01a whole application that has a specific interface. And you send your code to a new developer or in
08:08another scenario, you're you want to reuse that code, but you have another application that is
08:13already built and already ready and already working. And you want to read you want to
08:18use your class with class that's already in there, kind of maybe an animation class,
08:23or something of that nature. Now, you could in one route, rechange your whole class and rewire it and
08:29reconfigure it. Or another way you could just create a new class, a completely new class that
08:34has an API that your current application is expecting. And then internally, you could then
08:40rename stuff and send them to the right stuff and the right properties and the right things
08:44that you wanted to actually get it to work. So it's kind of that is actually something that's a
08:48specific design pattern that we're going to talk about a bit in a second. But, but spot on. Okay,
08:54so we're, I think it's time to dash through the rest because we're getting short on time.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!