I have a code like below where the Factory interface is implemented by two sub classes, in spring xml the bean defines itself as an object of one of the class(OrangeFactory in below example) while in getBean it tries to cast as AppleFactory, shouldn't getBean throw Class cast Exception during runtime in this case?
But Strangely it doesn't - what am I missing here?
public interface Factory<K, V> {
V get(K key);
}
public class AppleFactory implements Factory<String, Map<FCName, Boolean>> {
@Override
public Map<FCName, Boolean> get(String identifier) {
//doSomething and
return <some map>;
}
}
public class OrangeFactory<K, V> implements Factory<K, V> {
@Override
public V get(K key) {
//doSomething and
return <some map>;
}
}
and XML:
<bean id="appleFactory" class="OrangeFactory">
</bean>
Below method should throw ClassCast Exception during runtime? - But it seem to work fine...
public Factory<String, Map<FCName, Boolean>> getAppleFactory() {
return getBean("appleFactory", AppleFactory.class);
}