Tuesday, February 21, 2017

Inheritance

For many years I have driven a small red car. It has two doors, and windows on the front, sides, and back. It has four wheels that are filled with air, and a small wheel inside the car that turns two of the wheels when I turn it. It has an engine that makes it go, and a stick that lets me tell it to go forwards or not forwards. It even has two white lights on the front so I can see better when it is dark, and two red lights on the back so other people can see when I am trying to slow down.

I have spent many years learning how to drive this car, and I am very good at driving this car. But then one day, I had to move something big from one place to another, and it would not fit inside my little car, so I had to use a truck. I had never driven a truck before: it was very big, much bigger than my little car. But it still had all the parts my car had, only bigger! The engine was a little different: it ran by burning a different kind of stuff than my car's engine burns, but it still went forward and not forwards when I told it to. So I was able to drive the truck just by doing the same things as I do when I use my little red car.

Later on, a friend needed to get their big family to the place where the flying machines land but they didn't want to leave anything just sitting there while they were gone, so they asked if I could drive them there and then drive back to their house. There were too many of them to fit in my small car, so I had to drive a different one: their bus! They have a big bus that fits 18 people at a time! I had never driven a bus before, and this was much bigger than my little red car. But this bus still had four wheels filled with air and a little wheel inside that turned two of them when I turned it. And it still had an engine, and a stick that let me tell it to go forwards or not forwards. It even had the same two white lights on the front so I could see better in the dark, and two red lights on the back to show other people when I am trying to slow down. It was a little different: there were more doors than my car had and many more windows, but the wheels still turned when I turned the small wheel inside. So I was able to drive this bus just by doing the same things as I do when I use my little red car.



This is what we call Inheritance.

We're able to make one thing and add a lot of things it can do or things it can remember, and then make a different thing that Inherits from the first thing. When we do this, the second thing is still able to do everything the first thing does, even if it does it a different way (like how the truck burned a different kind of stuff from the little red car). The second thing can even do more stuff than the first thing, like how the bus had more doors, windows, and seats than the little red car.

The truck was still a car and the bus was still a car, and that's why I was able to drive them the same way, but they were also so much more and each of them did a very special job very well.

Monday, February 13, 2017

Encapsulation

As you are walking down the street, you see a person who has set up a table with a sign that reads "Yellow Fruit Drink for money". You have two yellow fruit but you do not know how to make them into a drink, so you decide to ask the person to make you some yellow fruit drink. You walk up to the table, give the person some money, and give the person your two yellow fruit: they take out a glass, press the two yellow fruit together and let them fill the glass, then they add something sweet to make the yellow fruit drink better, and then give you the glass full of yellow fruit drink. This drink is very good, so you come back again the next day and do the same thing.

After several days of this, you begin to notice that there are lots of other people doing this too, and it is taking a lot of time to get your yellow fruit drink, so you decide to help the person make yellow fruit drink faster and better. When you look at each step of making yellow fruit drink, you notice several things that could be better.

You see that some people are bringing three fruits or one fruit or some of them are bringing both yellow and red fruits and the person is having to figure out what to do with each person's fruit. So you go in front of him and make sure that everyone in line has exactly two fruit and only yellow fruit. This makes everything faster for the person, because they're working with the same number and kind of fruit every time.

You also see that the person is adding different things each time to make the yellow fruit drink sweet, so you get one big bag of sweet stuff so the person can just use the same stuff each time.

You also see they have different size glasses and have to make different sizes of yellow fruit drink each time, so you get a large number of the same size glasses so the person can make the same size yellow fruit drink each time.

You then see that the person is doing too much work for just one person, so you go get other people to help make yellow fruit drink. The first person takes the yellow fruit from the people in line and hands it to the other people to make into yellow fruit drink, and then the first person gives the finished drink back to the people in line. This makes the line go faster, but people do not like this as much: they saw a person and expected that person to make their yellow fruit drink. They don't like other things too: they want something different to make their yellow fruit drink sweet, or they want a different size glass. Since this only happens because the people in the line can see the person behind the table, you decide to build a box around the table so that people in line cannot see the people making the yellow fruit drink or the glasses or the sweet stuff. This works, and the people in line go back to being completely happy.

So now you have a big box next to the road with a sign that reads "Yellow Fruit Drink Maker" on it. As people walk up to it, they see two holes in the front of the box and a thing to push in the middle: the hole on the left has a sign that reads "Step 1: Add two yellow fruits"; the thing to push has a sign that says "Step 2: Press this"; the hole on the right has a sign that says "Step 3: Take yellow fruit drink".

Now things are much easier! The person inside the box can change anything they want to, as long as they take in two yellow fruits and give back a yellow fruit drink. They could change the sweet stuff to something that needs less money, or they could change the other people who are helping make the yellow fruit drinks to other people or even to machines, and nobody outside the box minds because they are still getting their yellow fruit drink.



This is what we call Encapsulation.

We find some way to point out only what our work will accept coming in and what our work will always give back, and we hide the rest. We could do any kind of work we want to inside the box, but as long as everyone in line can count on us to keep taking the same things in and giving the same thing back, they don't care how we do it inside! This lets us make sure that the work we are doing inside is the easiest, or the fastest, or uses the least stuff, or whatever is most important to us inside the box, without anything outside the box trying to tell us what to do.