java - When inserting objects into a type-safe heterogeneous container, why do we need the class reference? -


i'm checking out heterogeneous container pattern bloch's effective java , i'm trying determine why class reference needed when inserting objects heterogeneous container. can't use instance.getclass() reference? isn't jpa's entity manager example of this?

interface blochsheterogeneouscontainer {      <t> void put(class<t> clazz, t instance);      <t> t get(class<t> clazz); }  interface alternativeheterogeneouscontainer {      // class<t> not needed because can use instance.getclass()     <t> void put(t instance);      <t> t get(class<t> clazz); } 

no can't that, won't give class of reference type in case of inheritance, rather class of actual object type.

consider example:

number num = new integer(4); system.out.println(num.getclass()); 

this print:

class java.lang.integer 

and not java.lang.number.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -