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.

Thursday, January 26, 2017

Abstraction

Imagine that, here in front of me, I have a box called "Apples".
Someone will hand me an Apple, and I will place it in the box.
This happens twice, so now the box has 2 Apples in it.

But then someone hands me an Orange, and expects me to put it in a box. I can't put that in the box called Apples! It's not an Apple!
So I get another box, put it in front of me, call it "Oranges", and put the Orange in it. Great! Now I have a place to keep Oranges when they get handed to me.

Life continues for a while, with someone handing me an Apple that I put in the Apples box or an Orange that I put in the Oranges box. But then someone hands me a Pear. As you can imagine, I now have a "Pears" box in front of me.

And then someone hands me an Apricot... and I have an "Apricots" box now.

And this continues: each time someone hands me something new, I have to go get a new box and call it something new. Now I have a lot of boxes!

So all of this time while someone has been handing me Apples and Oranges and Pears and Apricots, once in a while they'll ask for one back: "Give me one Apple" they say, and I reach into my Apples box, pull one out, and hand it to them. "Give me the second Apple" they say, and I can reach down to the bottom of my box of Apples and pull out the next-to-last one there, because the one at the very bottom is the first Apple I put in there so this is the Second Apple.

But then they ask me for something I'm not expecting: "Give me the third thing I handed you." All this time, I've been keeping everything handed to me sorted by what sort of thing it is: an Apple, an Orange, and so-on. I have not been keeping track of when they handed it to me! The only way for me to do that would be to put everything in the same box, but I can only put something in a box if it is what the box is called!

The fix is simple: call the box something that can cover Apples, Oranges, Pears, and Apricots. I need a name for all of those things, so I grab a box and call it "Fruit". Perfect! Now we can put all of those Fruits into the same box. When they ask me for the third thing, I can hand it to them no matter what sort of thing it is. When they ask me for the second Apple, I'll pull out things until I find an Apple, then pull out more things until I find the second Apple and give them that one!




This is what we call Abstraction.

We find some way to call several different sorts of things by how they are like each other. They could all share the same color, or they all come from trees, or they're all food, or whatever! The key is that we can call all of them by that thing that makes them like each other, so we can make a box that uses that thing to call them.

When we need one certain kind of thing from our box of abstract things, we need to check each thing to see if they have the right thing we're looking for.