Polymorphism Part 3 Tutorial

In my previous polymorphism tutorials I conceptually introduced you to the principle of virtual method invocation, aka polymorphic method invocation. At runtime, the JVM determines which method to invoke based on the type of object, not the type of the reference variable – and that is the virtual in virtual method invocation. It is important to understand that polymorphic method invocation only applies to instance methods. It should go without staying, but overriding instance methods in subclasses is critical for virtual method invocation to even be performed. Because polymorphic method invocation only applies to overridden instance methods, there are quite a few rules that need to be learned about overriding methods. Future tutorials will cover each rule in great detail. The purpose of this tutorial is to demonstrate that polymorphic method invocation only applies to instance methods.

  • An instance method does not contain the static keyword in the declaration statement.
  • A class method (or static method) does contain the static keyword in the declaration statement.
Can a static method be overridden? The technical answer is NO, but there is more to it than just that. When we declare a static class method with the same signature in a subclass we are not overriding the method, we are hiding the method. I will be going into more detail on the topic of hiding methods in a future tutorial. A hidden method is called based on the reference variable type, not the object type – polymorphism doesn't even apply.



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

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


class PolyThree {
    public static void main(String args[]) {
        Car c1 = new Car();
        Car c2 = new HondaAccord();
        Car c3 = new ToyotaPrius();

        c1.averagePrice();
        c2.averagePrice();
        c3.averagePrice();

        System.out.println();
        c2.averagePrice(); // polymorphism invokes the method based on the object (instance) type
        c2.newCarSmell(); // static method (class method) invokes the method based on the reference variable type - no polymorphism
        //HondaAccord ha = new HondaAccord();
        //ha.newCarSmell();

        //System.out.println();
        //System.out.println("Remember, we can also directly call static methods ...");
        //Car.newCarSmell();
        //HondaAccord.newCarSmell();
    }
}

class Car { // implicitly extends object
    void averagePrice() {
        System.out.println("The average price of a new car is $28,400.");
    }
    static void newCarSmell() {
        System.out.println("Nothing quite like that new car smell!");
    }
}
class HondaAccord extends Car {
    @Override
    void averagePrice() {
        System.out.println("The average price of a new Honda Accord is $26,007.");
    }
    //@Override
    static void newCarSmell() { // method hiding - uncomment the @Override and see
        System.out.println("Hum ... is the new car smell really different based on the model???");
    }
}
class ToyotaPrius extends Car {
    @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 PolyThree.java and press Enter.
Now type in java PolyThree and press Enter.


C:\Java\PolyThree>javac PolyThree.java
C:\Java\PolyThree>java PolyThree
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 Honda Accord is $26,007.
Nothing quite like that new car smell!
Hum ... is the new car smell really different based on the model???

Remember, we can directly call static methods or invoke them by reference.
Nothing quite like that new car smell!
Hum ... is the new car smell really different based on the model???


Final thoughts

The one thing to take away from this tutorial is that polymorphic method invocation only applies to instance methods.


Tutorials