By:Mohammad-Ali Bandzar | March 3 2019
A java tutorial that shows you why coding java
(or any other object oriented programming)
is just like writing a Spongebob Episode.
-or-
How to understand Java by looking at pretty colors.
Learning Object Oriented Programming concepts in Java can be difficult. This rendition of Johannes Clarbouts Don’t Fear The OOP! Will attempt to explain object oriented Programming using analogies to Spongebob on how these concepts work.
Similar to Claerbout, our text will be color coded. Green is the most basic introduction, and is written in sentences. Yellow slightly more advanced and essentially pseudo code. Red is more challenging and is written as proper code. Words that are bolded are important terms that are used by programmers.
Meet Mr. Squarepants, Mr. Squarepants lives a life underwater. His TV show is a documentary about his life as a fry cook underwater. Spongebob's viewers are never disappointed because his shows when watched back to back appear as one continuous episode. How does he manage this?
Well, before Spongebob's creators produce anything, they first write their ideas down at their desks, sweep aside the empty 5 hour energy bottles. It is on this single page which they will plan Spongebob's whole life.
How can Spongebob's creators get the whole story onto only one page? Simple - everything they produce onto the page is simply a series of events or plots. Setting, character development, and how the characters interact is all taken care of elsewhere. How do they do that? Let's find out.
You see, Spongebob's creators, more than anything else, are very consistent in the TV show. It’s what has made it so popular. Let's say that they have sat down to produce their hundredth episode.
The first thing that the producers need is a setting, so they dust off an old binder they have labeled “Underwater Towns”.
The description of an Underwater Town, although not perfect, performed two key tasks: 1) it established the key ingredients in a quality Underwater Town and 2) It provides several pieces of information that makes the town more memorable.
UnderwaterTown Has a certain number of sponges Has a certain number of houses Is located somewhere Exists at a certain time BikiniBottom would have: Number of houses = 1 Location = North of Goo Lagoon Time period = 2000’s
It may seem that Spongebob's creators, the producers, have a strange way of writing, but to them it makes sense. The first line states that they are defining an UnderwaterTown. Notice that they do not use a space between the words. This is because they fear that a space may confuse their stickler artists.
The next step, logically, is to declare the types of variables (things that define how the Underwater Town looks) that a class of an Underwater Town might have - such as houses and a certain number of sponges etc. That being said, all that's left to do is to construct a sample town, with default values for its variables.
Public class UnderwaterTown{ int numberOfSponges; int numberOfHouses; String location; int time; Public BikiniBottom(){ numberOfHouses = 1; location = “North of Goo Lagoon”; time = 2000; } }
In making Bikini Bottom, the producers have defined a class, but not Bikini Bottom in particular (that would be an object). A class is like a cookbook recipe without the measurements - it says what should be included in the Underwater Town, but not what quantities to include them in.
This is the process of declaring what the variables will be called, and what sort of values they will contain. For now, just worry about "ints”, "Strings” and “booleans”. "int" stands for integer, "String" for a string of letters, and “Boolean” refers to a true or false statement.
The semicolon is the producers’ way of letting each other know that they are done with the preceding statement and that they have moved onto the next one. For the most part, you can think of it like a period.
Finally, you may have noticed that whenever the producers want to group a particular set of statements, they will use curly brackets, “{”. Even though they all know their characters well, this lets them quickly see where statements begin and end. Any class will always end with all the open curly braces being closed.
If the producers do create (instantiate) Bikini Bottom (object) eventually in their TV show, their artists will look to the same Underwater Town class to determine what that town should look like (The producers will send their binders full of classes to their artists. But where will their artists find out the number of sponges and such? This is where we get into the constructor.
The constructor comes after all of the variables have been declared. It starts with public BikiniBottom(). The statements that follow are the default values for their variables; how the producers’ Bikini Bottom object would look like if they had simply instantiated it in the TV show without specifying any of its values.
So now, we’ve sneaked a peek at the producers’ binders. They have reviewed the Underwater Town section and think that this episode should take place in an underwater town. Now they are ready to write the first part of their show. They turn to the main page of their binders and
In just a few lines, the producers have conveyed a lot of information. When they send the information off to their artists along with the binders of information, the artists will look up “BikiniBottom” and will be able to fill in information that is constant about the town, such as the number of sponges. In addition, the publishers have added information about the number of sponges. Because the number of sponges wasn't specified within the binder, the artist was happy to see how many there are specified in this plot. Although the show’s plot only has two lines, because of their reference to their binder, their TV show already contains quite a bit of content.
Main Visit: Underwater Town BikiniBottom is a new Underwater Town The number of Houses is one The number of sponges is two The number of grandmas is one
The producers take two very important steps very early on in their plan, they instantiates an object of type BikiniBottom. Having created their first object, they go on to fill out the information that was initially lacking in the UnderwaterTown class: (Number of houses and number of sponges).
Public class VisitingGrandma{ Public static void (String arguments[]){ UnderwaterTown BikiniBottom = new UnderwaterTown; BikiniBottom.numberOfHouses = 1; BikiniBottom.numberOfSponges = 2; BikiniBottom.numberOfGrandmas = 1; } }
The publishers have a strange way of saying things, don’t they? It would be possible to explain what each word in those first two lines do, but it wouldn't make much sense to you at this point, and it would really tax my small brain, so let’s skip it for now? What’s important to know is that that line is how the producers let their artists know that this piece of paper is the main routine, or plot.
When the producers decided to specify those variables (in this case, BikiniBottom. Remember that BikiniBottom is an object of UnderwaterTown) Followed by a period then by the name of the variable. Having done this, the publishers can then provide a value for the variables such as 2.
So, believe it or not, you've just seen object-oriented programming in action. Our publishers have first created the class (that was the binder) that created a general definition of what an UnderwaterTown was, the producers then turned to their main plot layer and created (instantiated) an UnderwaterTown object which they called BikiniBottom.
Now, for the grandma, the producers need a different binder. So they put “UnderwaterTown” back on the shelf, and go to pick up a new binder. They pass over many different titles, like “Pets”, “Restaurants”, “Weather”, “Music”, all the essentials for a good Spongebob episode, until they finally come to the binder they want: “Sponges”. They open up to the introduction:
You’ll notice that they like to make all body parts variables. This gives them the freedom to change their values for different characters later in the story. You’ll note that they left out the sex, name, and memorable shape as no two sponges are completely alike. The publishers prefer to specify those variables when they write the plot.
Sponges Have a certain number of legs Have a certain number of arms Have a certain number of eyes Have a certain number of mouths Have a name Have a certain sex Have a memorable shape A standard sponge would start off with Two legs Two arms Two eyes One mouth When someone asks them for their name Tell them your name.
You, being as sharp as you are, have probably noticed that the producers have developed some interactivity into the sponge characters of the show, when asked for their name, they will respond. This is called a method and is your key to a good time. We’ll get to it in just a few pages.
Public class Sponges { int legs; int arms; int eyes; int nose; int mouth; String name; String sex; String memorableShape; Public Sponges(){ legs = 2; arms = 2; eyes = 2; nose = 1; mouth = 1; } Public String identifyName(){ return name; } }
Wow, almost all of it should start making sense to you now, except for that “Public String identifyName()”. Don't worry, we will get into all that in just a little bit.
However, as interesting as these descriptions of humans may be, they’re not very specific. The publishers promised their artists a new story by Friday, so they decide to get down to business. Flipping through the “Sponges” binder, they come to the first chapter, titled “Grandmas”
Nothing really new here, except that we have peered deeper into the publishers’ binders (classes). This particular subsection, SpongeGrandma, extended the idea of sponges.
SpongeGrandma extends the idea of Sponges. A SpongeGrandma Has wrinkles Has a dress Has a unique hair color Wears glasses Has a unique personality Has a number of grandchildren visited in a day Will knit sweaters For a given Sponge Grandma, She will be nice. She will wear glasses. She will be wearing a dress. She will have white hair. She will start off the day with no grandchildren who’ve visited. She will start off the day with 0 sweaters knit.
Public class SpongeGrandma extends Sponges{ Boolean wrinkles; String dress; String haircolor; String personality; Boolean glasses; String personality; int grandChildrenVisited; int numberOfSweatersKnit; Sponges grandChildren; Public SpongeGrandma(){ personality = ”nice”; grandChildrenVisited= 0; numberOfSweaterKnit = 0; } }
We have introduced here the idea of subclasses. “Sponges”; was a class, and “SpongeGrandma” a subclass of it.
You may also be wondering why the publishers declared “Sponges grandChildren” in this subclass. Think of it this way, if the grandma is going to have grandchildren visit her, the artists have to know what a grandchild is. By declaring “Sponges grandChildren”, her editor will know that a grandchild is a type of sponge.
After the publishers have described what their sponge grandmas look like, they decided to move on to some of the methods (remember those from a few pages ago?) that grandmas use to knit sweaters. Being a quality production, the producers decide to allow the grandma to say how many sweaters she has knit.
While this may not look any different than any of the things that we were doing before, you should note that the publishers are specifying how one of their characters acts, rather than just how he/she/it looks. You should also note that they are altering one of their variables here, the variable “numberOfSweatersKnit”. SInce their binders (classes) can contain information about how their characters act, the publishers can create quite a bit of character development without ever even touching their main plot page.
knitSweaters Number of sweaters knit increases by one
Notice the pattern then has begun to emerge in the publishers’ writing. What they have done with this method is similar to how they treated variables in the past. First, they note what they are going to describe (in this case a method called knitSweaters) and then, on the next line, what the method will do when they call it in their main plot (routine).
public void knitSweaters () { numberOfSweatersKnit ++; }
Although the publishers used quite a few strange symbols, their approach was straightforward and consistent. The first thing that they did was naming the method “knitSweaters”. Since the method name is knitSweaters, the publishers figure that they should modify the grandma’s number of sweaters knit, in this case by adding one to it. That is what the “++” after “numberOfSweatersKnit” does.
Now, the grandmas in the producers’ tv shows are famous for being able to knit sweaters. Hence, it’s very difficult to see exactly how many sweaters the grandma has knit in her large stack. The publishers decide that it’s a good idea to allow their grandma to state how many sweaters she has knit, so, they write a new method:
howManySweatersKnit Tell them how many sweaters I have knit.
public int howManySweatersKnit (){ return numberOfSweaterKnit; }
If you’re wondering about that “int” in front of the “howManySweatersKnit()”, the publishers had to put that in because this method has a return value. In order to let the artists know what sort of value the can expect the method to return, they specified “int” (for integer) in front of the name of the method. Remember, their artists don’t like surprises, and if they didn’t tell the artists what sort of variable this method was going to return, they’d get all hot and flustered. (Now, we don’t want that, do we?)
At this point the publishers’ are pretty proud of themselves, having created a grandma that can knit sweaters. However, just to make for a good closing, the publishers decide to have the grandchildren visit the grandma. They set out to write one more method. What makes this different than the other methods is that it needs information about someone other than the grandma, namely the grandchild that is visiting. To allow for flexibility (and a variety of grandchildren), the publishers decide to leave the identity of the grandchild blank for now.
So now you can see the flexibility that the publishers have in writing their stories; they can leave certain things as variables to be specified only when the plot is written. And by writing that the grandchild will have a name, but not stating what it will be, that’s just what they have done. They have also used a print statement that will print out the information gathered by the method as an event in their book. How easy!
visitGrandma (name) Add one to the number of grandchildren who have visited. print “Oh golly (name_grandchild) has visited my old soul!”.
Public void visitGrandma (Sponge name){ this.name = name; grandchildrenVisited++; System.out.print (“Oh golly” + name.identifyName () + “has visited the my old soul!”) }
“Geez! I should have stayed with the green type,” you’re probably saying to yourself. Don’t worry. It looks much more than it is, and you probably understand most of it already. First, we have the name of the method: “visitGrandma”. No surprise there. Then, between those parenthesis, we have “Sponge grandChild”. What comes between those parenthesis is called an argument. It helps make the method more specific. When the publishers start writing their, plot, they will at some point want the grandchild to visit the grandma. But if they just write in the plot “grandma.visitGrandma()”, that wouldn’t be very exciting for the viewer. Who has visited? What was their name? Priding themselves on always having strong characters, the publishers wants to add the name of the grandchild who is visiting. Hence, when their plot says “visitGrandma”, it will also say the name of the grandchild that is visiting as an argument. “Sponges”, which comes before “grandma”, tells the artist that the thing visiting is a sponge. The artists don’t like surprises, so knowing that it is a sponge visiting, rather than say, plankton, reduces the guesswork on their hands.
The next line is another attempt by the publishers to please their artists, who are being even more picky than ever! Even though they have supplied the name of the grandchild as an argument, their artists won’t let them use it in the method until they have stated that the “grandchild” that she declared as a “grandma” variable is equal to the vale given in the method’s argument.
Having finally jumped through all of their artists’ hoops (don’t expect to understand all of that right away, it took the publishers a couple of weeks!), the publishers get down to finishing the rest of their method. Once the grandchild has visited the grandma, the variable “grandChildrenVisited” will surely go up by one, so the producers put the same “++” after grandChildVisited that they had put after “numberOfSweatersKnit” in the “knitSweaters” method.
The stage being set, the publishers decide that it is time to let their readers know what is going on. Hence the “system.out.println” statement. A bit wordy, but it let’s their artists know that what comes between those parentheses goes into the final plot of the episode: all the text they put in between quotation marks. Why is the variable ‘grandchild.identifyName()’ then not in quotation marks? Because it is not a literal, but rather an object’s method. By keeping it out of the quotation marks, the publisher lets their artists know that they have to for look for the grandchild object (they would find out that it was of the “Sponges” class) then for the method “identifyName” (they would see that it returned the grandchild’s name). All of that for a grandchild’s name!
Had they put grandChild.identifyName() inside of the quotation marks, their artists would have drawn out the words in the final copy of the episode: “Oh golly grandChild.identifyName () has visited the my old soul!”. In order for their artists to substitute the variable name for the word name, the publishers close the quotation marks before they write it. The “+” lets the artists know that the publishers want them to join the two statements.
Lucky for you, the next page is just review.
At this point, it might be a good idea to review what the publishers overall description of grandma looks like. None of this material is new, just a compilation of what we’ve gone over in the past few pages.
Grandmas extends the idea of Sponges. A Grandma Has wrinkles Has a dress Has a unique hair color Wears glasses Has a unique personality Has a number of grandchildren visited in a day Will knit sweaters For a given Grandma, She will be nice and compassionate. She will wear glasses. She will be wearing a dress. She will have whitehair. She will start off the day with no grandchildren who’ve visited. Will start off the day with 0 sweaters knit. knitSweaters Number of sweaters knit increases by one howManySweatersKnit Tell them how many sweaters I have done. visitGrandma (name) Add one to the number of grandchildren who have visited. print “Oh golly (name) has visited my old soul!”.
Public class SpongeGrandma extends Sponges{ Boolean wrinkles; Boolean dress; String haircolor; String look; String glasses; String personality; int grandChildrenVisited; int numberOfSweatersKnit; Sponges grandChildren public SpongeGrandma(){ wrinkles= ”true”; dress= ”true”; hair= ”white”; look = ”compassionate”; grandChildrenVisited= 0; numberOfSweaterKnit = 0; } public void knitSweaters () { numberOfSweatersKnit ++; } public int howManySweatersKnit (){ return numberOfSweaterKnit; } public void visitGrandma (Sponge grandChild){ this.grandChild = grandChild; grandchildVisited++; System.out.print (“Oh golly” + grandChild.identifyName () + “has visited the my old soul!”) } }
All this work with Grandmas and sweaters has the publishers feeling better then ever. So, they decide to develop the main plot.
Finally, they set their pens to the sheet of paper labeled main routine.
Here is the main plot of Visiting Grandma at Grandma’s house; in the TV show Spongebob; There is an UnderwaterTown called BikiniBottom; BikiniBottom has one grandma; BikiniBottom has two sponges; BikiniBottom has one house; There is a new grandma named Granny; Granny has wrinkles; Granny has white hair; Granny doesn’t wear glasses; Granny has a frilly dress; Granny is round in shape; There is a new Sponge named Spongebob; Spongebob is male; Spongebob is rectangular in shape; Granny knits a sweater; Granny tells us how many sweaters she’s knit; Spongebob visits Granny;
public class visitGrandma { Public static void main (String arguments[]){ UnderwaterTown BikiniBottom = new UnderwaterTown (); BikiniBottom.numberOfHouses = 1; BikiniBottom.numberOfSponges = 2; BikiniBottom.numberOfGrandmas = 1; SpongeGrandma Granny = new SpongeGrandma (); Granny.sex = “female”; Granny.wrinkles = true; Granny.hairColour = “white”; Granny.glasses = false; Granny.dress = “frilly”; Granny.memorableShape = “round”; Sponge Spongebob = new Sponge(); Spongebob.sex = “male”; Spongebob.memorableShape = “rectangular”; Granny.knitSweater(); System.out.println (Granny.numberOfSweatersKnit()); Granny.visitGrandma (Spongebob); } }
THANKS FOR READING