Polymorphism and Instance Variables Tutorial

In my previous polymorphism tutorials I discussed the principle of polymorphic method invocation. At runtime, the JVM determines which method to invoke based on the type of the object, not the type of the reference variable. Since polymorphism applies to instance methods, does polymorphism apply to instance variables? The answer is no, polymorphism does not apply to instance variables.

Can we override an instance variable just like we can override an instance method? Technically NO, but there is more to it. While it is true that subclasses inherit members of a superclass – and that includes accessible instance variables – simply declaring an instance variable in the subclass with the same type and name merely 'hides' the inherited superclass instance variable.



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

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


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

        System.out.println(c1.price); 
        c1.averagePrice();  

        System.out.println(c2.price); // price is displayed based on the reference variable type - no polymorphism
        c2.averagePrice(); // method is invoked based on the object type - polymorphism!

        System.out.println(c3.price);
        c3.averagePrice();

        System.out.println("---------------------");

        HondaAccord ha = new HondaAccord();
        ToyotaPrius tp = new ToyotaPrius();

        System.out.println(ha.price);
        System.out.println(tp.price);

    }
}

class Car { 
    int price = 28_400;
    void averagePrice() {
        System.out.println("The average price of a new car is $" + price + ".\n");
    }

}

class HondaAccord extends Car {
    int price = 26_007; // not overriding - simply hiding the superclass price variable
    @Override
    void averagePrice() {
        System.out.println("The average price of a new Honda Accord is $" + price + ".\n");
    }
}

class ToyotaPrius extends Car {
    int price = 23_450; // not overriding - simply hiding the superclass price variable
    @Override
    void averagePrice() {
        System.out.println("The average price of a new Toyota Prius is $" + this.price + ".\n");
    }
}


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


C:\Java\PolyInstance>javac PolyInstance.java
C:\Java\PolyInstance>java PolyInstance
28400
The average price of a new car is $28400.

28400
The average price of a new Honda Accord is $26007.

28400
The average price of a new Toyota Prius is $23450.

------------------------
26007
23450


Final thoughts

Polymorphism does not apply to instance variables.


Tutorials