Polymorphism Part 1 Tutorial

The rules for polymorphism can be somewhat confusing so I will be presenting a mini-series covering the details of each rule. I highly recommend watching my Introduction to Polymorphism tutorial before continuing. The topics for this tutorial are:

  • A reference variable must be declared as a specific type.
  • That reference variable may be assigned to an object of that specific type, or any object that is a subtype of the declared type.



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 PolyOne
C:\Java>cd PolyOne
C:\Java\PolyOne>Notepad PolyOne.java

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


class PolyOne {
    public static void main(String args[]) {
        Car c1 = new Car();
        Car c2 = new HondaAccord();
        Car c3 = new ToyotaPrius();
        Car c4 = new FordMustang();
        Car c5 = new ShelbyGT350();

        c1.averagePrice();
        c2.averagePrice();
        c3.averagePrice();
        c4.averagePrice();
        c5.averagePrice();
    
        System.out.println();
        
        Car cArray[] = { new Car(), new HondaAccord(), new ToyotaPrius(), new FordMustang(), new ShelbyGT350() } ;

        for(Car temp : cArray) {
            System.out.println(temp.toString());
        }

    }
}

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

class HondaAccord extends Car {
    @Override
    void averagePrice() {
        System.out.println("The average price of a new Honda Accord is $26,007.");
    }
}

class ToyotaPrius extends Car {
    @Override
    void averagePrice() {
        System.out.println("The average price of a new Toyota Prius is $23,450.");
    }
}

class FordMustang extends Car {
    @Override
    void averagePrice() {
        System.out.println("The average price of a new Ford Mustang is $25,800.");
    }
}

class ShelbyGT350 extends FordMustang {
    @Override // comment this method and run to demonstrate the hierarchy.
    void averagePrice() {
        System.out.println("The average price of a new Shelby GT350 Mustang is $59,400.");
    }

    @Override
    public String toString() {
        return "The 2016 Shelby GT350 has a whopping 526 horsepower!";
    }
}


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


C:\Java\PolyOne>javac PolyOne.java
C:\Java\PolyOne>java PolyOne
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.
The average price of a new Ford Mustang is $25,800.
The average price of a new Shelby GT350 Mustang is $59,400.

Car@2a139a55
HondaAccord@15db9742
ToyotaPrius@6d06d69c
FordMustang@7852e922
The 2016 Shelby GT350 has a whopping 526 horsepower!


Final thoughts

Review this tutorial over and over until you finally accept the concept of assigning a reference variable to a subtype of the declared type. Once you have accepted how this all works, please move on to my next tutorial.


Tutorials