Encapsulation is another aspect of Object Orientated Programming that you're going to read and hear about, even when you start to delve into programming languages like C# or VB.NET or any kind of programming language actually. That is involved with Object Orientated Development. And so in this video, I want to talk about Encapsulation, it's a very simple concept but again these concepts while they're simple to understand and you kind of give it a nod. Yeah okay, I got that. When you begin to write code, it's amazing how easy it is to slip away from these things and kind of fall victim to some very common nerd mistakes if you will. Okay, so Encapsulation is one of the most important aspects of Object Orientated Programming or OOP as you'll see it and hear it out there. And that is simply this, that a class should hide the complexity of it's operation from the user and I could add here, as much as possible. Let's think about an oven. When I use an oven, do I know how it makes it work? Well obviously not I'm not an oven engineer. Okay. If those people exist and obviously there's some out there somewhere. All I need to know how to do, is turn the oven on and open the door. Everything else is time. Right. If I leave it too long, it burns. If I don't leave it long enough, then it doesn't get done enough. Okay. So that's all I should understand about an oven. Now operation of the oven is Encapsulated away from you. I turn a little a knob to tell it to take it to 450 degrees, it happens. I don't know anything about thermostats. I don't want to know anything about thermostats. I don't want to have to study thermostats, I just want my pizza cooked. Right. And so that is Encapsulation. The functionalities of an oven, the operation of the oven is Encapsulated away from me, but the operation of the oven is exposed to me by the interface that's offered and in this case, it's the knobs. Now Encapsulation also involves the accessibility of the internal functionality. We'll talk about this later, when we talk about Modifiers on Properties, on Methods, on Classes. They can be public, they can be private, they can be protected. We'll get into that later. But that's part of the whole Encapsulation scheme. There's a lot of things that might happen, if I'm trying to transfer data across the Internet. But do I want the end user to be concerned? No, I want them to type an URL or an IP Address and everything gets taken care of for them. Now it may take me a couple of weeks of banging out some code, you know and almost losing my sanity and finally figuring it out and getting it to work. But in the end, when a user when uses my class, they simply, they pass some information, they call a method and boom, it's just happening. Now what does Encapsulation give us? Well the main thing is, it supports the concept of code reuse. Now we've all kind of been led to believe that operating in an Object Orientated Programming Environment is going to let us continue to build objects and build objects and build objects, until the day comes that the boss comes in and says hey, this is the application I want and we say, no problem. I'll take this object that I've already built and this object that I've already built and this object that the other people built in the other department and this object that this lady over here in Human Resources built and I'm just going to tie them together with a few lines of code and boom it's done. It's all working, I'm out of here, gone by 1 O'clock. Playing golf by 1.30. Okay. I've never actually seen that happen but it is possible that we can reuse a lot of different code. The Oven Class could easily be used in somebody else's project. Why? It's written in the .NET Framework. As long as they're writing in any .NET compliant language, they could take the Oven Class that we wrote. They could place it into their particular application of their project, Set References to it and boom they should be able to use it. And that user does not need to understand how the oven works. They don't need to understand on the inside, how our Oven Class is working. They just need to know how to call the functionality to make it happen. That's what Encapsulation is and that's the way we should be constructing our classes.
| Course: | Microsoft C# 2010 |
| Author: | Mark Long |
| SKU: | 34306 |
| ISBN: | 978-1-61866-037-4 |
| Release Date: | 2012-03-19 |
| Duration: | 8.5 hrs / 105 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |