3 ms·
Java like C++ or Swift uses single-dispatch polymorphism, so only the receiver (the argument before the '.') is used at runtime. In Java, you can implement you
by _old_dude_ 6y ago
Java like C++ or Swift uses single-dispatch polymorphism, so only the receiver (the argument before the '.') is used at runtime.
In Java, you can implement your own dispatch table quite easily
Map<Class<?>, Consumer<Fruit>> dispatchTable = Map.of(Apple.class, fruit -> handleFruit((Apple)fruit));
Fruit fruit = basket[0];
dispatchTable.get(fruit.getClass()).accept(fruit);
- pjmlp 6y agoIn C++ you can fake multi-dispatch, by making use of templates, templates specialization and overloading though. It is just somewhat cumbersome.