Don't Fear the OOP
In this assignment, we had to rewrite the original story "Don't Fear the OOP". The original story used terms that were not family-friendly nor appropriate for school. We rewrote the story using school-appropriate words, while still keeping the key ideas. Here is our rewritten story, and hopefully it will help you better understand the concepts of Object Oriented Programming (OOP).
There are three different levels of this tutorial, coded by color. Green is for those readers who want the most basic introduction. It is targeted at those who are unsure what object-oriented programming is, and could use a good analogy to make things clearer.Yellow is for those who want to be able to understand object-oriented programming just enough to be able to read and follow it, but are not yet ready to learn the intricacies of coding Java.And finally, the third level, red, is for you daredevils who want to be able to program in Java, but just want to ease into it slowly.
In short, the green text gives a "plain English" version of the code that would be necessary, the yellow uses that English in a way that more closely resembles the format of code, and the red is the actual code that would be necessary for the program to work. Readers of all levels are encouraged to skip between the colors to deepen their understanding. Finally, although this tutorial operates mostly through analogy, innuendo, and intrigue, those words that appear in boldface are the actual terms used by Java programmers (ooooh!), so try to remember them as you go along.
Wilhelm and his brother Jakob were university students living in what is now Germany. Together they were in the process of writing Kinder-und Hasmärchen, which is now known as Grimms' Fairy Tales. While working on this masterpiece they encountered a problem. How would they fit all the fairy tales they knew into one book?
When the first issue came out in December 1812 it contained 86 fairy tales, but by the seventh edition, that number was increased to 211. But including more stories came at a cost, they would have to make the book longer thereby using more paper which would drive production costs up.
Then Wilhelm had an idea, he would write down each story on a single page. Everything he wrote on the page would be just a sequence of events or plot. The setting, character development, and how his characters interact is all taken care of elsewhere. He was able to do this because of the overlap between fairy tales. Let's say that Wilhelm and his brother were just about to write "Cinderella", their twenty-first story in their book.
The main character in Cinderella, is, of course, Cinderella herself. But Cinderella is very similar to some of the other female characters in different Grimm fairy tales (Snow White, Sleeping Beauty, Rapunzel, etc.) ao he pulls out a binder full of princesses (and princesses to be), opens to the first page, and reads the first few lines:
So now we've sneaked a peek at one of Wilhelm's binders. He has reviewed the Fairy Tale Princess section and thinks that this fairy tale's main character should be, not surprisingly, a Fairy Tale Princess. So he's ready to write the first part of his plot. Wilhelm turns to the main paper that is sitting in the middle of his desk and starts to write the story.
So, believe it or not, you've just seen object-oriented programming in action. Our author Wilhelm first treated a class (that was the binder) that roughly described a fairy tale princess, then turned to his main plot page and created (instantiated) a princess object, which he then called cinderella.
Now for the other characters, Wilhelm needs a different binder. So he puts "Fairy Tale Princesses" back on the shelf, and goes to pick up a new binder. His hand passes over many different titles, like "Animals", "Palaces", "Witches", "Ball Gowns", all the essentials for a good fairy tale, until he finally comes to the binder he wants: "Secondary Characters". He opens up to the introduction:
As interesting as these descriptions of secondary characters may be, however, they're not very specific. Wilhelm promised his readers a new edition by Friday, so he decides to get down to business. Flipping through the "Secondary Characters" binder, he comes to the first chapter, entitled "Sisters".
After Wilhelm had described what Cinderella's stepsister looked like, he decides to move on to some of the methods (remember those from a few pages ago?) that sisters employ. Being a teetotaler, Wilhelm wants to focus on want happens when a sister buys a dress. So he writes:
Now, the sisters in Wilhelm stories are famous for the number of dresses they own. Hence, it's very difficult for an onlooker to gauge how many dresses one of his sisters really owns. Wilhelm decides that it's a good idea to allow his sister to state how many dresses she owns, so, he writes a new method:
At this point Wilhelm is pretty proud of himself, having created a sister that can experience pretty much the full range of sister activities. However, just to make for a good closing, Wilhelm decides to have the sister have the ability to meet princes. He sets out to write one more method. What makes this different than his other methods is that it needs information about someone other than the sister, namely the "prince" to meet princes. To allow for flexibility (and for a variety of "prince"), Wilhelm decides to leave the identity of the "prince" blank for now.
At this point, it might be a good idea to review what Wilhelm's overall description of a sister looks like. None of this material is new, just a compilation of what we've gone over in the past few pages.
All this work with princes is making Wilhelm feel a bit run down, so he decides to stop working on his characters for a little while, and to work a bit on the main plot.
Accordingly, he sets his pen to the sheet of paper labeled main routine.
"That's WIlhelm's book?" you ask yourself. "Ten years of learning german for this?"
So what is so special about this plot? Well, it's not everything that's happening in the main routine, but rather everything that happens behind the scenes. When Wilhelm created the princess named Cinderella, he created (instantiated) an object of type Fairy Tale Princess. This object had all the characteristics of a standard Fairy Tale Princess (do you remember the binders?). The same thing goes for the Anastasia object and the Adam object. While some of the traits of these objects were specified at the time of creation, most of their traits were specified back in the binders, or classes. This allowed Wilhelm to say a good deal while only saying a little bit in his main routine.
So Wilhelm's main plot (routine) turns out to be nothing more than a collection of references to objects, which in turn are references to classes.
So what are the benefits of this? Well, imagine that instead of one page, Wilhelm's reader wanted a story that was 50 pages long. Or let's say Wilhelm decided that Anastasia the stepsister shouldn't be so mean to Cinderella. Instead of going back and laboriously changing all his instances of Anastasia taunting Cinderella, and locking her in the basement, Wilhelm could just make some changes to the Anastasia object at the time of instantiation. Or he could change the default settings of the Sister class to be nicer to the main character.
But that's not Wilhelm's only reason for writing his books in Java (his writings were considered odd, even by 19th century german standards). He has a keen idea for the future of fairy tales. He sees interactivity. And if instead of being bound in pages, his characters live in objects and classes, Wilhelm is free to create a choose your own adventure by leaving some blanks in the book. Whenever readers need to refer to attributes of the characters they would see the values that they themselves added and not the values that Wilhelm decided on. They would be able to find out if the story would have played out differently if Anastasia had three legs instead of two, or if Cinderella had black hair.
You've seen many references to Wilhelm's readers. So who are they? Well, they know the world of Fairy Tales in and out, and he knows that certain things will just not work in a good Fairy Tale. That is why the readers take all of Wilhelm's scripts, reads them, and then return them with all the errors that he has made (remember, we said that 19th century Germans were a finicky bunch). On the computer, this is called a compiler. All Java programs must be compiled before they can be run. The compiler will patiently (and repeatedly) tell you everything that you did wrong. And then you get to go back and do it again. Hey, that's the literary life.