Introduction to Polymorphism

What is polymorphism? Hearing this term for the first time may invoke thoughts of some sort of super complicated alien shapeling technology from a Star Trek episode. In reality it is a fairly simple concept as long as lingo like "one interface many types ... blah, blah, blah" is not used to define it. Take a minute to think about your family members, how are you related to your biological cousins? Well, for one, you and your cousins both have at least on biologic grandparent in common. You and your cousins both have certain traits that have been genetically passed down from that grandparent. You inherit biological characteristics from your biological ancestors.
Things like your eye color or hair color are examples of inherited characteristics. Without going into great detail about dominant and recessive genes, consider the following example. Suppose both of your grandparents had blue eyes, then your mom inherited the recessive blue eye gene and has blue eyes as well. Suppose that your father has brown eyes, then there is a high probability that his dominant brown eye genes will overwrite the recessive blue eye gene and that you will have brown eyes. Your aunt or uncle (on your mom's side) would have blue eyes as well, your cousins will inherit their eye colors in part from your grandparent's genes, but also from the other family tree involved as well. If we blow this example up to all of humanity, then we end up with an interesting finding. We all have eye color and there is only about six overrides that apply to eye color.

Back to the wonder world of Java OOP. Let's say we have a class called Car that contains a method called averagePrice(). Suppose class HondaAccord extends Car, then the HondaAccord will inherit all accessible members from the Car class. Now suppose that ToyotaPrius extends Car, then the ToyotaPrius will also inherit all accessible members from the Car class. We can say with 100% certainty that both the HondaAccord and the ToyotaPrius have the averagePrice() method. The best way to show you what we can do with this is to present you with a code example.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md PolyIntro
C:\Java>cd PolyIntro
C:\Java\PolyIntro>Notepad PolyIntro.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class PolyIntro {
    public static void main(String args[]) {
        Car c = new Car();
        HondaAccord ha = new HondaAccord();
        ToyotaPrius tp = new ToyotaPrius();

        displayAverage(c);
        displayAverage(ha);
        displayAverage(tp);
    }

    static void displayAverage(Car param) {
        param.averagePrice();
    }

}

class Car {
    void averagePrice() {
        System.out.println("The average price of a new car is $28,400.");
    }
}

class HondaAccord extends Car {

}

class ToyotaPrius extends Car {

}

        //Car ha = new HondaAccord();
        //Car tp = new ToyotaPrius();
    //@Override
    //void averagePrice() {
    //    System.out.println("The average price of a new Honda Accord is $26,007.");
    //}
    //@Override
    //void averagePrice() {
    //    System.out.println("The average price of a new Toyota Prius is $23,450.");
    //}


Now switch back to the command prompt (CMD) and type in javac PolyIntro.java and press Enter.
Now type in java PolyIntro and press Enter.


C:\Java\PolyIntro>javac PolyIntro.java
C:\Java\PolyIntro>java PolyIntro
The average price of a new car is $28,400.
The average price of a new Honda Accord is $26,007.
The average price of a new Toyota Prius is $23,450.


Final thoughts

This tutorial just serves as an introduction to the concept of polymorphism. Stay tuned for my next few tutorials which will introduce you to more ways to integrate polymorphism and certain rules as well.


Tutorials