Java Basics: Polymorphism

I had previously covered one of the four fundamentals of Java programming: Abstraction. Today I am going to cover Polymorphism, based on Greek words “poly” and “morph” compounded into “many forms” from translation.

No not Polymerization, Polymorphism!

One of my favorite hobbies personally is cooking so I’ll be using that as an example, along with code snippets of course! Don’t forget to rinse your vegetables before dicing them.

class PanFry {
    public void panFry() {
        System.out.println("1: Begin heating a clean frying pan to medium heat.");
        System.out.println("2: Lubricate pan.");
        System.out.println("3: Season with salt, pepper, and fry until brown, occasionally moving about using spatula or wooden spoon.");
        System.out.println("4: Turn off pan heat, take pan off heat, wait until pan stops emitting hot steam, finally serve and enjoy!");
    }
}

First we create our main class, PanFry. Its method panFry() will instruct on how to pan fry successfully. Notice how step 2 seems a bit vague? Let’s try to pan-fry both broccoli and a diced onion.

class Broccoli extends PanFry {
    public void panFry() {
        System.out.println("1: Begin heating a clean frying pan to medium heat.");
        System.out.println("2: Lubricate pan with olive oil.  Add broccoli, drizzling a bit extra on top five minutes in.");
        System.out.println("3: Season with salt, pepper, and fry until brown, occasionally moving about using spatula or wooden spoon.");
        System.out.println("4: Turn off pan heat, take pan off heat, wait until pan stops emitting hot steam, finally serve and enjoy!");
    }
}

class Onion extends PanFry {
    public void panFry() {
        System.out.println("1: Begin heating a clean frying pan to medium heat.");
        System.out.println("2: Lubricate pan with butter, melting fully in pan before adding diced onion.");
        System.out.println("3: Season with salt, pepper, and fry until brown, occasionally moving about using spatula or wooden spoon.");
        System.out.println("4: Turn off pan heat, take pan off heat, wait until pan stops emitting hot steam, finally serve and enjoy!");
    }
}

Oh! But what’s this? The method originally drawn from the extended PanFry class diverges in both subclasses? This is what we call overriding, translating the same method to multiple different forms. Using olive oil to pan-fry diced onion is gross, so we prefer to use butter for this vegetable. Step 2 is different in both subclasses, an example of overriding i.e. overwriting the original method’s behavior. Behaving differently is polymorphism.

Another example of polymorphism could be overloading if our method had parameters. Overloading changes the method’s structure changes its parameters. If the panFry() method had an Integer parameter, using it in an extended subclass with an additional String parameter is overloading.

class Kitchen {
    public static void main (String[] args) {
        PanFry sideDish = new Broccoli();
        PanFry steakTopping = new Onion();

        sideDish.panFry();
        steakTopping.panFry();
    }
}

Using our kitchen, we create new objects from our extended subclasses to be used in our main method. Next, all we must do is call these object’s specific methods we polymorphed by the extended class’ method name. Before you know it, we have enough for a steak dinner! Who’s cooking the steak?

I suppose lamb is good too!

Hopefully you may now understand Java polymorphism, or at least understand it before your next meal. Bon-appeti!

My video on this subject

Follow my original code on my GitHub profile!

Leave a comment

Design a site like this with WordPress.com
Get started