Basic Programming Principles (OOP) for Beginners ✅ | Grant Klimaytys | Skillshare

Basic Programming Principles (OOP) for Beginners ✅

Grant Klimaytys, Software Engineer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
13 Lessons (1h 3m)
    • 1. Introduction and what you will learn

      1:05
    • 2. What is object oriented programming?

      3:29
    • 3. The big idea, why do most modern languages use object orientation?

      2:14
    • 4. Setting up our development environment

      2:24
    • 5. Classes and objects explained

      3:17
    • 6. Creating a class and an object

      11:27
    • 7. Inheritance explained through an example

      7:34
    • 8. Types of inheritance and gotchas

      5:03
    • 9. Polymorphism explained

      2:20
    • 10. An example of polymorphism

      11:23
    • 11. Encapsulation explained

      1:51
    • 12. An example of encapsulation

      5:01
    • 13. Project brief and farewell

      5:35

About This Class

Object oriented programming. What is it?

That was my biggest question when I started programming.

It took me many months to get the object oriented programming concept fully cemented in my mind. Now I will help you get object orientation in less than an hour. Avoid needlessly complicated explanations that you find on the internet, as I explain everything in PLAIN language and Lego illustrations!

__________REVIEW OF THIS COURSE_____________

Very Simple and Interactive way of learning Basic of OOP

_________________________________________________

Object Oriented programming (OOP) is used in almost every popular language out there, be it Java, C#, Javascript or PHP. You’ll also need it for iOS and Android apps if you make those!

So ENROL TODAY!

0f786364

Transcripts

1. Introduction and what you will learn: Hi, My name's Grant, and in this course I'm going to teach you all about object oriented programming, and it's something you're going to see in every modern language like Java, sea shop and Swift. Now, I remember when I was first learning about object orientation, it took me a good six months to a year to actually understand the fundamental pillars of what it waas. So way back then, when I first started learning, I had no idea what classes and objects where inheritance, polymorphism and encapsulation. But they're all really simple topics. And that's why I've done this course is so that you, as the beginner, can learn all of these topics in less than an hour because they are so simple. I also use Lego illustrations because I find that's the best way to get something as a picture is worth 1000 words and who doesn't like Lego? So I look forward to working with you 2. What is object oriented programming?: alrighty. Hello there. In this tutorial, I'm going to walk you through just exactly what object orientation is in terms of programming. And when I started programming, it was extremely confusing to me. But once you get the idea of it, you'll never go back to any old way of thinking. And to ease your transition into how I explain this, I'm going to be using Lego pieces. And Lego is the perfect waits described programming, because when you program you're creating something. When you use Lego, you're creating something. So what was programming like when it first started? Well, programming. When it first was around Waas, I had one piece of data and I want to feed it through a pipeline, and I want to do various operations on it. So say I had a number. I would feed it through an ad one bit at a time. And in Lego, we can represent that with three workers one, two and three, and in the left end, we give the first worker a job. To do so, he has to make, for example, the chassis of a racing car. Once he's done that, he passes it on to the second worker, and he adds the wheels to the racing car. And finally, they pass it on to the sad looking guy on the right, and he adds the spoiler on the seat and various other bits of the car. And once that's done, the car leaves the factory, and that is the end off the production line. So in this way of thinking we're thinking about the workers were concentrating on those guys. But what happens if we make two cars or three cars or 20 cars or 20 million cars? Well, we start to have a problem if we want to keep track off those cars because our program focused only on the workers. We don't really know where those cars have gone. And in the early days of programming that was fine because we only had one piece of data generally that we wanted to analyze. So object orientation is still programming, but it puts the focus on the item being built instead. Off the work is doing the work, so object orientation is. Here's the chassis of the car, and one by one we invite the workers through this door to come and add bits to the car, so they do that bit by bit eventually. And then in the end we have a car that we're keeping track off. We don't really care about the workers where they stay and what they do. We only care about the final object that's produced and that is all. There really is toe object orientation. We're concentrating on the thing that we make. 3. The big idea, why do most modern languages use object orientation?: Okay, Welcome to lecture to another short one. And in this one, we're just going to look at the big idea. Why do most modern languages use object orientation? Well, if you recall from lecture one, we had three guys on a work line and this was the old way off building things in programs. And those three guys could make a car like that. When we switched object orientation, we had the car as the focus. And we have the three guys in turn, creating the bits on the car. Now, if we go back to the really old way of doing things, What if I wanted to change something on that car? What if I wanted to put two seats on it and a much larger spoiler? Well, I'd have to train some new guys and insert them into this little work line, and that would be a problem, because that would mean writing more code in our program. We don't necessarily want to do that. And, of course, as usual, that's not the whole truth. There are ways around that, but it is much, much simpler to go down an object oriented route. So when we start to focus on the object. The guys can be switched out as they come through the door on. Each guy is now a standalone unit, and he can do different things to the car so we can produce a car like that instead. And object Orientation is so, so powerful primarily because off this reason, we can do things toe objects using little bits of code. And we don't necessarily have to worry about the order of the code, where the codes stored or how the code does its job. And in the next lesson, we're going to install a little bit of programming software called Mono Develop that lets us develop on Mac, PC or Lennox and run all of those applications. Cross platform. So join me then. 4. Setting up our development environment: Okay, let's go ahead and get our development environment set up, and we're going to use the very popular mono develop. And this is an open source port off the dot net framework, I believe, and it runs on OS X windows on Linux. Now. Currently, I know about this because I use what's a product called Samir in and Xamarin allows me to write cross platform APS for android IOS Windows, phone Mac windows, etcetera. It says up here that you can download Xamarin studio, which is a development environment allowing you to use mono develop, and you can use it free as long as you don't want to create I us or Android APS. I do do a separate course on creating IOS and Android APS. So if you want to learn that, then go ahead and look at that course later on. So for now, come to mono developed dot com slash download on hit download Samrin studio. And once you've done that, run it and it will verify everything for you. And once that's complete, just drag xamarin studio into applications and it will be installed. Okay, it will be very similar on Windows. You'll just have to go through an installation process. Alternatively, you can also follow the tutorials using Microsoft's visual studio, and obviously this is only going to run on Windows and you can download it from visual studio dot com. This is also completely free for individual developers. And as we go through the various examples in this course, the workflow is going to be extremely similar between the two platforms. So it doesn't really matter which one you have. If you already have one, then simply use the one that you do have. Okay, now that you've got everything installed, I look forward to seeing you in the next lesson when we're going to discuss classes and objects which are the fundamental core off object orientation. So join me then. 5. Classes and objects explained: okay. During your programming career, you're going to come across classes and objects on. They are almost one in the same thing, and that's why they can be very confusing to people. But if I put it very simply once again, in Lego terms, a class is simply a blueprint. So a class in Lego would be an instruction manual by itself. It doesn't do anything inherently useful. But once you. Instead, she ate that class. Once you use it to create something, you'll come up with an object at the end. So a class is simply a blueprint that can create an object. That's not all the classes. A class can also have things inside. It called methods that allow the object to do things. So in this little mine sweeper that we've got up here, if I wanted to rotate these arms here when I'm doing my mind sweeping, I would have a method inside my class that allows me to rotate that I would probably call it rotate or something like that. I would also have a method that allows me to move my tracks, a method that switches on my lights at the front, a method that lowers and raises my mind sweeper and also in the class we have what are called properties. So in the class, the properties would say These bricks are yellow. This break is black, these ones are blue, so you can think of a class as a really, really advanced instruction manual, a kind of instruction manual with a primitive brain. So the instruction manual shows you how to build something. But it also allows you to rotate things, move things, do whatever you like with object that you've created Now. The other difference between a class on an object is that on the whole, a class does not change. The blueprints stay the same, and there is only ever one class for an object. We cannot have two that are named the same objects. On the other hand, we can have hundreds of thousands off those if we like, and later on in your programming career, you'll start to learn how to handle these objects and be very careful off something called memory leaks. But that's an advanced topic, and we will cover that later on. So in the next lecture, we're going to go ahead and create a class. Andi in Stan. She ate on object, which is the official terminology for building an object. And we're going to do that in our development environment. So see you then. 6. Creating a class and an object: Okay, let's create our very first class and object. Let's open up xamarin studio. Once that's fired up, come up here to new solution or corrupt the top and click file New solution. And you'll come up with a template chooser for your project. I want you to ignore for now all of these app based projects that is only available in the paid version of Hammerin and we're not going to look at that. Instead, we're coming down to other and we're gonna click dot net and we have a bunch of projects here. An empty console library, etcetera, etcetera. We're going to run a console project because a console project allows us to simply put text onto a console window, which is like your terminal window oil command window. It's a click next on that and we will call this. I don't know. Let's call this bulldozer, not bull Doc. Sir, Bulldozer. There we go and store it wherever you like and click create and Zaman will create all the files and folder structure that you need for the project. And so at the minute it has. If you look on the left hand side, three folders references packages properties. We won't worry about that for now. And one file called Program Don't CS If we come over to the right hand side in the window, will notice it's already put something called a class in Here for us. Now we're not going. Teoh. Look at that for now, But just note that object oriented languages are going to use this keyword class a lot. An inside class. We have a method which is called Main, and inside this method it's simply going to write Hello World to the console. So if you come to the top left corner and you hit play on that, it's going to open up a console for us here and once it's opened it up, it types. Hello World press any key to continue, and it says that because the program has finished running, so we press enter and it says, I have saved your session. Now if I came back to Xamarin Studio and I said hello, Mars instead and then I ran that he would come over and say, Oh, hello, Mars so you can see what it's doing. This console dot right line is simply out putting text to our console. Okay, let's create a class, and our class is going to be the blueprint for creating a new bulldozer. And to do that, we're going to type public class. Oh, Dozo, and you'll notice. I've put a Capital D here, and this bulldozer up here has a small D, and we're just going to avoid clashes. There wouldn't be any clash if we named it the same, but in C sharp or any object oriented language for that matter, you always want to name your things slightly differently if you can, or completely differently, which would be even better. And inside our public class bulldozer, we're going to put a curly bracket press enter a few times and another curly bracket. Now this has defined a blueprint. At the moment, the blueprint does nothing because we haven't written anything. So let's put some properties into bulldozer. First, let's define a color. So let's say public string color and then opened. Curly brackets get set and closed curly brackets, and that just simply means that everything that uses this blueprint can use this property called color. Let's set another property. Let's say public string sighs, and we will write this as get sent again. Okay, so we have a bulldozer blueprint and the blueprint allows us to specify the color and the size of our bulldozer. So that's our class written, but we haven't actually created are bulldozer yet. We still only have our blueprint. So to create it, we're going to come up here to this main method And before console that right line, we're going to say var does. That is equal to new bulldozer. And you have to be very careful here. You have to choose the one with a capital D here, like so put your brackets in and semi coda. So this variable on the left hand side has now created a new bulldozer using this blueprint . And if we want to assign some colors or sizes to that dozer, we can then say dozer dot color equals two. Let's call the 1st 1 yellow close quotes, semi colon. Let's give it a size and say dozer don't sighs is equal to many semi colon. And then in our consul dot right line, we're going to clear this Hello Mars and we're going to say I want you to show me the following Seibu does, uh, is close quotes and then right plus dozer. Darks color like So Save that and let's run it. And once it's run, we have this. It outputs to our console and says, Your bulldozer is yellow. So just to summarize, this is a class. It's the blueprint. It does nothing by itself. Until you instance, she ate that class and you're going to use the word in Stan. She ate a lot in your program in Korea, and once you've in Stan, she hated it. You've made a copy from that blueprint, and you can assign the various properties to your dozer. And I'm just going to show you why Object orientation is so so good. If I wanted to create another dozer, I could just save our does. A two equals new bulldozer like so, and I can give this one a dozer dot Color equals read, and I could give it a size. Dozer dot sized is equal to large, but you'll notice were doing something here, which is namely, we're repeating code. If I wanted to create. 100 of these does is I have to write does a dark color 100 times, which would be very, very annoying. But what we can do instead is to use what's called a constructor on a constructor is something we can place in our class and typically replace it at the top of our class. And we call this public bulldozer and it has the same name as the class. So C Sharp knows that this is our constructor and if we open our bracket, we can type. The first constructor I want you to give me is underscore color. The 2nd 1 I want you to give me is underscore sighs open curly brace and then close curly brace. Then inside this constructor, we're going to say this which is a special key word that refers to this class. And we're going to say this dot color is equal to underscore color and we're going to do the same with size is equal to underscore size. So now when this method is called, it's going to assign this color to this color down here and the same thing with size here. And all of that means we can now get rid off these lines here, which are very, very annoying, like so. And if we come back here, You say new bulldozer open brackets. It now asks us for color and size. So I will say yellow comma and many like so and we'll get to do the same for does too. So we will say red and notch like so And so you can see we've already cut down half of our code in this main method. And so if we want to run that we can say bulldoze of one is does a dark color and then we can write a new line and say Bulldozer to is does a two dot color. And if we run that we have a bulldozer. One is yellow bulldozer to is red, so that pretty much sums up classes and objects. And we've uncovered one bonus thing for you, and that is the constructor inside a class. So a constructor is just a method that takes a bunch of information that you want to put inside your object, that you're going to build in the next lesson. We're going to cover a new topic, which is inheritance and inheritance is closely linked to this idea, off objects and classes. So join me then 7. Inheritance explained through an example: Okay, guys, let's move on to the next topic in object orientation. And that is inheritance. In the last lecture, I briefly covered that we don't really want to rewrite code. Every time we do something in a program, it's really inefficient. And we'll get very annoying very fast for you as the programmer. So to help you understand this concept of inheritance that allows us to write less. I've got three Lego objects here. I've got a man, I've got a cart, and I've got this weird alien thing that I totally didn't just make up right now. And I'm gonna ask you what is the similarity between these three things? And it's not obvious at first because this is just a still picture. But all three feet, all three of these things move. They all move through three dimensional space. So if we come over to Samrin on what we're going to do is delete everything in this main class here, come down to our bulldoze the class and we are going to change this class name too, man. And then we have to change this public name too, man, and remove this constructor here. Remove this line here and we have to give the man some properties. So we're going to give him a property which is called Bull and Boolean just means true or false. It only has two values it can be. And this value for bullying is going to be moves, so it allows us to define If a man can move or not move, we're going to give him some other things, like a string name. And maybe we will give him a color like so right if we want to then define a cart, we can write public class, cut public. I can't like so and then we have to give the cart some properties. So I'm going to copy some of these lines here and we want moves, that's for sure. The cart won't have a name. Maybe we will call it manufacturer. And finally, the cart will have a color. And let's close off that class without curly brackets. Then copy that and let's define our alien. So we'll say alien alien and they're alien. We want to define if he can move or not. That's fine. I don't think they have my new factories, but we can't say for sure. So we will say planets that he comes from. And finally we will say color. Even though he's multicolored, his main color really is this yellowish color here. Okay, now we can see if we want to create a new man we would save Our man equals new men like so And we would do the same with cart and with Alien, and that would be absolutely fine. So we say far card equals new cart and we save our alien equals new alien like So Now we have a man, a cart on an alien that is being produced from our blueprints here. But the eagle eyed amongst you will have noticed. I've put moves and color in every single one. And again, we're writing too much code. So this is where inheritance is going to come in on. The first thing we have to do to use inheritance is create a new class. Let's do it at the top here. So we will say public class and we have to give this class and name that makes sense in terms of moves and color, all makes sense in terms off these objects. So perhaps we will call this class object that moves and has color. Now, that's a bit for boasts. And if you were going through your program later on, you'd have to stop and stare at that for a while to understand what it is. But in my world, it's always much better to be verbose than not. Now that I've said that, let's actually shorten this to just general object. And of course, you can call it anything you like, But it has to make sense for your particular program that's open our brackets and close our brackets. And of course, we need a constructor public general object. And I'm not going to put any stuff in there for now because we covered that previously. Then we have to give this one some properties, and we're going to give it all of the common properties that are classes have. So the 1st 1 is likely to copy moves, and I'm gonna copy color like sir, Then what I'm gonna do is I'm gonna come down to these three classes and I'm going delete moves and delete color, and I'm gonna do that old way down in all of them. So now these three classes have three different names Planet manufacturing name and that's great. We haven't repeated any code, and we have these moves in color here, and somehow we have to transfer the properties off this general object to the man, the cart and the alien. The way we do that is through inheritance. And if you click on man space Colon, you can type general object. And what this means is that if you create a man first, it's going to look at general object and steal all of general objects properties and put it inside man. And wherever I type that, that is going to be the case like so. So if I come up to my main up here and let's say I want to give my alien a color, I would say alien dot and you'll notice it gives you the option for color moves and planet , even though in Alien it's not specifically written inside the class. What it's done is take those properties from general object that pretty much covers inheritance. But there were a few gouaches that I'm going to cover in the next lecture on inheritance, So join me then 8. Types of inheritance and gotchas: Okay, let's look at the few ways we can inherit because we don't necessarily have to inherit from one object we can inherit from multiple objects. So just come up to this main class first and elite Carnelian to meeting up our program and save it. Now we have a general object and everything. The man carton alien. Those three guys all inherit from general object so they can access moves in color. What if I had a class called woman? Well, I could say public plus woman, and this one would have string name. We would have a get set like So There we go. Let's not forget our constructor like So Okay, now again, you should know by now that we have to inherit from general object and we have a woman. But we have a problem. We have a name in man and we have a name in women. And of course, we don't want to keep writing name. So let's come up here and create another class called Human except an inside human. We can say public human. I know we're not using the constructor for now, but it's good practice to put it in and inside this, we can say public string name get sent. Except so now we have a human and we can use that class for the man and woman to inherit from. So we can delete this name here like that, and some languages will allow you to inherit from two classes. So human comma human. Let's save that and run it. And we have a problem. It says bulldozer dot man classes cannot have multiple base classes. General object and bulldozer dot human Now, the reason for that is that C Sharp doesn't like inheriting from two classes and there's a simple idea behind that. And that is, if I had a moves property inside here that would get very, very confusing. I'm going to undo that. Some languages do allow it, but be very, very careful when you come across it. The correct way to do this is to create a hierarchy of inheritance. So what we're going to do is removed this human here like so come up to our public class of human and we're going to use that to inherit from general object. So now human is going to take the properties from general object and If we come down to man and woman and replace that with human, it is now going to inherit all of those properties. So if you will, it forms kind of a tree. I'll just show you that tree pictorially here. If I take this human class and I tab it in and I take man and woman classes and I tap those in twice. First, we're getting a general object from moves in color. Then we're getting a human object that has a name. And then we're making a man and woman out of that down here. On the other hand, cart and alien only inherit this stuff that's in general object. So that's hierarchal inheritance. And it's something that you're going to use a lot. So whenever you see repeated code, just be aware that you should be using inheritance in the next section. We're going to cover another concept of object orientation called polymorphism. So I look forward to seeing you then 9. Polymorphism explained: Okay, let's move on to another very, very important topic in object orientation. And that is polymorphism and polymorphism is a big fancy word that confuses a lot of people . And at the base of it, Polymorphism is best explained once again with Lego. So let's say we have to factory workers here. These two guys three younger guy on the nonplussed guy and they work in a factory. And in this factory, little things come through the door that they have to do an action on. And they expect little blue things because they were trained toe only work with blue things . So what happens if non blue things come through the door? So here we have the door again and we've got red ones and the guys don't know how to use their tools on red ones. They fall over. And of course, in reality we'd know what to do. Is humans because we're just really advanced computers? But in the computer world, if you give an object to a worker and he isn't trained and doesn't know what to do with it , and it's not the kind of object he expects, everything's going to crash and nothing's going toe work. So what you have to do is you have to get your original to workers, put them through another training program and tell them to expect two types of object. And if you get one object, I want you to do one thing. If you get another type of object, I want you to do a different thing. And so now when we have those, they're perfectly happy and they haven't crashed. And that's basically what Polymorphism is. It's having effectively a worker and giving him different types of objects and him then being able to do specific things with those objects. So in the next lecture, we're going to look at how that's actually applied in our C sharp program, So join me then. 10. An example of polymorphism: Okay, let's do some Polly morphing in our program. Come over to Zaman. And if you've still got to a bulldozer program open, let's delete all of these classes down here that we created on. Let's delete everything inside Main. So we're just left with those lines. Now we're going to construct what's called a polymorphism, and we're going to use as an example I would Lego men. So first we have to create some workers. Let's create a class called factory worker and curly braces. And inside this class we're not going to give it any properties, so he won't give them names or colors or anything like that. What we're going to do is give it a constructor, which, of course, is a method. So I'll say factory worker Andi. Inside this constructor, I will pass nothing. There's no arguments inside here. Now let's say we want this factory worker to simply count the number of things passing by his station. So to do that, we're going to give him a method which will be public void count and then inside there, we're going to give him a number that he should add to his tally. So we're going to say int which is interject, which just means a whole number so minus 10123 etcetera And we say number open curly brackets and close curly brackets Andi to store this number and to add it, we have to make what's called the class variable. So let's come down here and say public int total get set like so and then inside of count, we're going to say if total is equal equal to know, that means it's not ready yet. Then we're going to set it 20 just to start with and then in future Total will never be no . So it won't execute this line on the next line. We're going to add our number two total. So we're going to say total equals the current value of total plus number. And then what we're going to do is we're going to write it to our console so we can actually see this happening right line total dot to string, and that just converts it to text for us. Here we go. So every time this count is run, it adds one to this total. So we know main, Let's come up here press tab. So because we've created a class as our blueprint, we now need to create a worker as the object equals new factory worker like so And let's give him a task. Let's say Worker, don't. And then up here we have count and in brackets. Let's give him a number to add. So let's say four on. Let's make him do it again because we're paying him and he has to do what we say. Make him at five and save that. And now let's run that and see what happens to our console. And if we look here, it's run the program. It's gone through once and added four and gone through again and added five, which gives a total of nine. So it's doing exactly what we expect. I also have an error here. I've said if the total equals no unreachable code, let's ignore that for now. After two Sierra on my part. Okay, the next thing that we have to do is look at polymorphism. So what if I have instead of four things, I have 4.5 things well, and it cannot count in halves, but there are other variable types that can counting halves for us. We can use what's called a float number. So let's try and do that. Let's come up here and say, Mr Worker, I would like you to count 4.5 like so And what happens when we press play is we get an error and it says the best overloaded method match for count has some in valid arguments. And if you click that, it says argument number one cannot convert double expression to type int. And that says this 4.5 I'm assuming is off type double. I'll just type that out for you double. If I hover over it, it should give us a tool tip, but it hasn't but nonetheless, double can count halves. But let's turn this into a float number. Let's put Unefon it 4.5 f. Let's run that And again it says, I cannot convert float to type int, and that's because it's coming down here to count, and it expects an interject. And if we come back to our Lego analogy, here is where we gave it. A ninja on the workers were perfectly happy here. We gave it afloat and the workers threw a tantrum. They don't know how toe work with floats. So we have to train these workers to work with floats as well as inter jizz. So let's come over here and fix that problem. Let's create another method called public void account. And if you recall in earlier lectures, I said, Never name your methods the same, But there is an exception, and that's when you're going to put in a different type of variable, and that's exactly what we're going to do here. So we're gonna put him a float number, like so open curly brackets and close curly brackets. Okay, then we want to add this number to our total, so total equals total plus number, and that's going to get this number here now, before we carry on, there is a problem. What we're doing is adding an integer and afloat, and they are different types. Even though they're both numbers, our compile it is going to have a problem with this. So what we have to do is convert everything, so come down to our variable here and write it as a float and then come up to our count here and what we have to do is right. Total equals total plus number and before number. We actually have to convert this to afloat, which is to single and then put number in brackets. And that converts it to the float, which means we can then add things and everything plays nice. And the final thing to do in this float number is console dot right line, total dark to string and that will tell us what our current tally is. So let's hit play and there we go. We now have four and nine and a 13.5, which perfectly matches what we have here. So this here this method off count float is only cold whenever something up here is past afloat. If it's passed an integer. So this four and five, then this method is called and that's is polymorphism. It's basically having two methods, each with a different input type that allow you to do the same thing so I could continue down this route and I could say public, avoid count and I could say I have an integer num one. I have a float number two and I have a double number three And then I can write some code in here to add all three. And I can add those to the total add to total like so and so up here. If I said I would like you to say worker dot count if you hover over count it asked Sorry, if you delete count and press dot and then you come to count down here It tells you I can either accept afloat and interject or I can accept three numbers integer afloat, a double. And there are a few other ways you can implement polymorphism, but for the moment, that is the core way that you're going to come across it in your programming career. 11. Encapsulation explained: Okay, guys, let's discuss encapsulation. Andi, in Lego terms in calculation is a factory, a very small factory like we have here. And this factory has an entrance here and our parts go in there and the factory has an exit here, and our finished product comes out now for you and I as the average Joe, we don't particularly care what goes in. We only care about what comes out. And if we give the factory a bunch of pots for a car, we expect a car to come out the other end. We don't care if you open the door and all of the parts go to Hong Kong on, get sorted there and then get made in China and then get flown back to the US or the UK, and then the carcass passed out this door. We just care about what comes out. And so we have our little Lego man here. He's dropped the part seem and at the end, he's got himself a car. He also doesn't care what goes on inside that factory, and that's the basis of encapsulation. So the reason we use encapsulation is so that we can change the factory processes but still have the same outputs and in programming that is way more important than it would be in a factory setting. So in the next lecture, I'm going to give you a quick example off what encapsulation is and why it's so important. 12. An example of encapsulation: Let's create our encapsulation example in our factory worker program. Let's remove all of this code inside here and just leave the class and the constructor there and then come up here and elite those lines to just leave the worker equals new factory worker. Come back down to the class and let's give this factory workers something to do like we did before. Let's get this worker toe. Add two numbers for us. Let's do that. We're going to make a method which is public on this method is not going to be called void Void simply means that we're not returning anything. We're going to call this method and in count numbers. Or rather, let's call this ad numbers and we say int here because we're going to return something off type integer toe, whatever calls it. And if we're going to add numbers, we need to receive some. So we say int num one and into number two. Open our curly brackets and close them. Okay, Now we need to add those two numbers, so we're going to save. Our result is equal to number one plus number two, and because we're going to return an integer we have to specifically say it. We say return results like So if we come up to our main, we have our work up. Let's tell him to add our two numbers dot at numbers and let's say I want to add two and three for me, like so. And let's put that inside of variable. So that's a bar result equals workers don't add numbers to comment. Three. So the result here will be whatever's returned here. Finally, let's write that out so we can see it working. Don't write line. Result up to strength except and then we run that we have, ah, console that pops up and it's added two and three for us in the worker. Okay, that's fine. Now, if we come back here, can start to mess around inside. Add numbers so we can change this result as much as we like, we can say result equals result Plus 100. Like so And of course, we know that's the wrong answer. So let's correct it and say result equals result minus 100 to take it off and let's run it again. And you won't be very surprised at the output because the outfit will be the same as before five. Now, encapsulation. If you recall from our factory worker, the factory worker doesn't actually care what happens inside the factory and coming back to our program. It's the same here. There could be a 1,000,000,000 lines of code inside here, and everything up here wouldn't know and wouldn't care about that code on that is beautiful in programming because if you have a team of 10 people, one of those people will be working on your factory worker class. Another one will be doing your main program. And as long as the main program guy expects this result for add numbers, the factory worker guy could do whatever he likes in here. And more importantly, whenever he likes to do whatever he likes in here, and it won't affect this up here. So encapsulation is also sometimes called data hiding, and it just simplifies the whole build process for programming. So a little bit of probe off planning at this start off your app will mean that you save a lot of headache down the road and you can change things without causing any headaches for other people. Okay, that covers encapsulation 13. Project brief and farewell: Okay, guys, welcome to the final lecture in this series. And in the final lecture, I'm going to set you a little homework and your homework is going to be making a car production line on. We're going to cover everything we've covered in this course so far. Classes, objects, inheritance, polymorphism and encapsulation. And we're going to do it all in this one little project. So the very first thing I want to do is make a car class and it should have the properties of a color and a manufacturer. We did something similar earlier. Then I want you to practice in Stan Shih ating an object from that class. So I want you to make the car one object and I would like you tu. Instead, she ate with color read and manufacturer Toyota. Then you're going to practice inheritance. So I would like you to make and you saloon car class that inherits from car class and then inside the saloon car class, put in a new property which is called number of seats. Then you're gonna practice your polymorphism. So I would like you to write three different constructors to do the following and I'm just going to copy and paste this up and your three constructors. The 1st 1 is going to allow you to assign the number of seats only. The 2nd 1 is going to allow you to assign the number of seats and manufacturer only. And the 3rd 1 is going to allow you to assign the number of seats that manufacturer and color. Once you've created those constructors, you're then going to create three objects. Now just copy and paste this in. And here are your three objects. The 1st 1 is you're going to create a saloon cop with two seats. Then you're going to create a saloon car with three seats on manufacturer. Listen. And finally, you're going to create a saloon car three with four seats, manufacturer Ford and Color Purple. And to do that, each one of these. When you Winston she ate it should only take one line inside your program on. If you could do all of the above, then you are absolutely set for object oriented programming. So I'll just go through that again in pictures. You're going to create a car class and from that class you're going to Instead, she ate a car. One object. Then you're going to create a saloon car class that inherits from the car class and inside there you'll add a new property called number of Seats. Then you'll write three constructors to do the following constructor. One will allow number of seats to be applied, constructed to allows number of seats and manufacturer to be applied. Three number of seats, manufacturer and color. Then, finally, you're going to create three objects saloon car, one saloon, car two and three, all with different properties on. I haven't mentioned encapsulation, which is the last topic we covered. And if you recall, that's something goes into the method, something comes out and we don't care what goes between. But that's kind of implicitly covered. Whenever we create objects inside our constructor, we don't necessarily know what our class constructor is doing. So we have covered encapsulation in that way. Okay. And if you can do all of that, you're doing very well. On day. I would advise you to look out for my new courses, which will be coming up soon, which is all about how to learn, see shop, Java, JavaScript and PHP using Lego toys. Because I think it's a really great way to learn. So if you've enjoyed this course, please leave me a good review. If you haven't, please leave me a decent review and tell me what you didn't enjoy about it. And in the future, I will make the course small in line with what people might expect. So it just leaves me to say thank you for listening to me, and I hope you've learned something worthwhile.