Not understanding a Java polymorphism example involving HashMaps -


i'm rusty on java polymorphism.

if have class merchandise, , class clothing extends merchandise, why aren't able following?

hashmap<string, merchandise> stuff = new hashmap<string, clothing>(); 

when so, getting compilation error:

datastore.java:5: error: incompatible types: hashmap<string,clothing> cannot converted hashmap<string,merchandise>         public static hashmap<string, merchandise> tshirts = new hashmap<string, clothing>(); 

aren't clothing items merchandise items? ^

consider following scenario:

hashmap<string, clothing> clothing = ... hashmap<string, merchandise> merchandise = clothing; // suppose allowed merchandise.put("potato", new potato()); 

uh-oh, putting potato merchandise, has gone clothing, because reference same object!


Comments

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Apache Thrift Tutorial Error -