Don't Fear OOP

· Mohammad-Ali Bandzar

A Java tutorial that shows you why coding Java (or any other object oriented programming) is just like writing a SpongeBob Episode.

The analogy of this tutorial is simple: think of a Java programmer as a writer, composing a SpongeBob Episode. All of the characters and settings are “off-the-shelf”, and need to only be modified slightly to fit into a new episode. All that’s left to write is a script for a plot that pulls all those pre-existing elements together.

That, in a nutshell, is Java programming. Think of it as Dean Koontz for smart people. Now, that might be all you want to know. If so, thanks for stopping by! If things still could use some clearing up (perhaps by way of a couple dozen pages of examples), then read on!

Learning Object Oriented Programming concepts in Java can be difficult. This rendition of Johannes Claerbout’s Don’t Fear The OOP! will attempt to explain object oriented programming using analogies to SpongeBob on how these concepts work.

The Setup

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. 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.

Creating a Class

The first thing that the producers need is a setting, so they dust off an old binder they have labeled “Underwater Towns”:

Every Underwater Town has a few key elements: Houses, a couple of sponges and grandmas. Bikini Bottom will have 1 house, 2 sponges and 1 grandma and will be located just north of Goo Lagoon in the early 2000’s.

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.

public class UnderwaterTown {
    int numberOfSponges;
    int numberOfHouses;
    int numberOfGrandmas;
    String location;
    int time;

    public UnderwaterTown() {
        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.

Instantiating Objects

Now they are ready to write the first part of their show. They turn to the main page of their binders:

Granny gets a visitor. This story takes place in a house located within Bikini Bottom. It has 2 sponges and 1 grandma.

public class VisitingGrandma {
    public static void main(String arguments[]) {
        UnderwaterTown BikiniBottom = new UnderwaterTown();
        BikiniBottom.numberOfHouses = 1;
        BikiniBottom.numberOfSponges = 2;
        BikiniBottom.numberOfGrandmas = 1;
    }
}

The producers take two very important steps: they instantiate 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).

Creating Subclasses

Now, for the grandma, the producers need a different binder. They open up to the “Sponges” chapter:

“All sponges start out with two legs, two arms, a pair of eyes and a mouth. They are either male or female, have a name, and have a memorable shape. If someone asks for their name, they will tell you.”

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;
    }
}

You’ll notice 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.

Extending Classes

SpongeGrandma extends the idea of Sponges - they are nearly identical except for a few unique attributes: wrinkles to indicate their old age, they wear a dress, they have hair on their heads, wear glasses, and will have a unique personality.

public class SpongeGrandma extends Sponges {
    Boolean wrinkles;
    String dress;
    String haircolor;
    String personality;
    Boolean glasses;
    int grandChildrenVisited;
    int numberOfSweatersKnit;
    Sponges grandChildren;
     
    public SpongeGrandma() {
        personality = "nice";
        grandChildrenVisited = 0;
        numberOfSweatersKnit = 0;
    }
}

We have introduced here the idea of subclasses. “Sponges” was a class, and “SpongeGrandma” a subclass of it.

Creating Methods

After the publishers have described what their sponge grandmas look like, they decided to move on to some of the methods that grandmas use to knit sweaters:

Whenever the main plot says that a grandma has knit a sweater, the number of sweaters she has knit will go up by one.

public void knitSweaters() {
    numberOfSweatersKnit++;
}

Now, the grandmas in the producers’ TV shows are famous for being able to knit sweaters. Hence, they decide to allow their grandma to state how many sweaters she has knit:

public int howManySweatersKnit() {
    return numberOfSweatersKnit; 
}

Methods with Arguments

What makes this next method different is that it needs information about someone other than the grandma, namely the grandchild that is visiting:

If the grandchild is supposed to visit the grandma, visit the grandma and then add one to the number of grandchildren visited. Then print out “Oh golly! (the specified grandchild) has visited my old soul!”

public void visitGrandma(Sponges grandchild) {
    grandChildrenVisited++;
    System.out.print("Oh golly " + grandchild.identifyName() + " has visited my old soul!");
}

What comes between those parentheses 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. Who has visited? What was their name?

The Main Routine

Finally, they set their pens to the sheet of paper labeled main routine:

Here is the main plot of Visiting Grandma in Bikini Bottom: there is an Underwater Town called Bikini Bottom. Bikini Bottom has 1 house and 3 sponges. There is a round grandma sponge named Granny. Granny has wrinkles, white hair, doesn’t wear glasses and has a frilly dress. SpongeBob is a male sponge. He is rectangular in shape. In our story, Granny starts out by knitting a sweater. She then lets everyone know how many sweaters she’s knit and then her grandson, SpongeBob visits.

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";

        Sponges Spongebob = new Sponges();
        Spongebob.sex = "male";
        Spongebob.memorableShape = "rectangular";

        Granny.knitSweaters();
        System.out.println(Granny.howManySweatersKnit());
        Granny.visitGrandma(Spongebob);
    }
}

THANKS FOR READING