Polymorphism Part 2 Tutorial

I highly recommend watching my Introduction to Polymorphism and my Polymorphism Part 1 tutorials before continuing. The topics for this tutorial are:

  • The object type that a reference variable is declared as will directly affect which methods it may invoke.
  • We can use a technique called casting to invoke methods from subclasses that are not known to the reference variable superclass type.
  • An interface can be the type for a reference variable.
  • The instanceof operator can be used to determine if a object is a subtype of another class or interface.
  • Refresher · a 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 PolyTwo
C:\Java>cd PolyTwo
C:\Java\PolyTwo>Notepad PolyTwo.java

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


class PolyTwo {
    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();
        Car c6 = new TeslaS();

        c1.averagePrice();
        c2.averagePrice();
        c3.averagePrice();
        c4.averagePrice();
        c5.averagePrice();
        c6.averagePrice();
        
        System.out.println();

        TaxIncentives ti1 = new ToyotaPrius();
        TaxIncentives ti2 = new TeslaS();

        ti1.displayRebate();
        ti2.displayRebate();
    
        System.out.println("\n---------------------\n");
        
        System.out.println("And now the fun begins...\n");

        Object cArray[] = { new Car(), new HondaAccord(), new ToyotaPrius(), new FordMustang(), new ShelbyGT350(), new TeslaS() } ;

        for(Object temp : cArray) {
            if (temp instanceof Car) {
                ((Car) temp).averagePrice();
            }        

            if (temp instanceof TaxIncentives) {
                ((TaxIncentives) temp).displayRebate();
            }
        }

    }
}

interface TaxIncentives {
    public abstract void displayRebate() ;    
}

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 implements TaxIncentives {
    @Override
    void averagePrice() {
        System.out.println("The average price of a new Toyota Prius is $23,450.");
    }

    @Override
    public void displayRebate() {
        System.out.println("The Toyota Prius qualifies for a $4000 tax rebate.");
    }
}

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

class TeslaS extends Car implements TaxIncentives {
    @Override // comment this method and run to demonstrate the hierarchy.
    void averagePrice() {
        System.out.println("The average price of a new Tesla S is $75,000.");
    }

    @Override
    public void displayRebate() {
        System.out.println("The Tesla S qualifies for a $6500 tax rebate.");
    }   
}


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


C:\Java\PolyTwo>javac PolyTwo.java
C:\Java\PolyTwo>java PolyTwo
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.
The average price of a new Tesla S is $75,000.

The Toyota Prius qualifies for a $4000 tax rebate.
The Tesla S qualifies for a $6500 tax rebate.

---------------------
And now the fun begins...

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 Toyota Prius qualifies for a $4000 tax rebate.
The average price of a new Ford Mustang is $25,800.
The average price of a new Shelby GT350 Mustang is $59,400.
The average price of a new Tesla S is $75,000.
The Tesla S qualifies for a $6500 tax rebate.


Final thoughts

At this point if you are feeling a sense of confusion, that is perfectly normal. My recommendation would be to review the Introduction to Polymorphism and watch it a few times and then wait a day. Sometimes your brain just needs time to solidify concepts and a good night of sleep will do just that. The next day watch the Introduction to Polymorphism again, and then move on to Polymorphism Part 1 and watch it a few times and then wait a day. Repeat this process and the concepts will sink in. We all want to learn everything fast, but some concepts just take time. The more frustration you feel, the more you are learning!


Tutorials