Timeline for Downcasting in order to call method of subtype
Current License: CC BY-SA 4.0
4 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 9, 2019 at 18:26 | comment | added | kondziu | It doesn't solve anything in terms of typechecking but it sticks to open-closed principle. Adding new class implementing particular interface doesn't require any change in the class with loop. Problem here is not only typechecking but downcasting too. Some says that downcasting is justified only if any execution path puts into downcasted variable an object of type that we downcast to. So are we forced to use separate collections to conform all mentioned rules? But then if we want to add new type then we need to add new collection so we are breaking open closed principle. | |
| Oct 8, 2019 at 16:03 | comment | added | Joop Eggen | @MartinMaat David is on the right track however: one can dynamically inspect for some feature (interface) of the object that it can deliver (or not). That can easily be more flexible than inheritance. In java <T> Optional<T> lookup(Class<T> interfaceType) for dynamic capability lookup.. | |
| Oct 8, 2019 at 14:34 | comment | added | Martin Maat | This does not solve anything in terms of type checking, does it? You are doing the exact same thing, just without inheritance because you are that guy who is super-allergic to inheritance and thus forbid yourself to use polymorphism . Yes, interfaces are more flexible. But sometimes you want something rigid to support you in order not to fall on your pretty face. | |
| Oct 1, 2019 at 12:00 | history | answered | David Arno | CC BY-SA 4.0 |