gusl: (Default)
[personal profile] gusl
I just ran into problems because of Java's static (non pun intended) typing. Say we have 3 classes: A extends B extends C

We have an object named instance whose class is A.

When instance.inputMatcher(x,y) gets called, we would expect the Java dispatcher to look for the implementation of inputMatcher in this order:
* inside A
* inside B
* inside C

This is indeed what happens if the inputMatcher methods are non-static.

If they are static, however, Java will insist on calling the inputMatcher() from C, because that is the declared type of instance.

Therefore, we need to down-cast. For example:
if (matcherInstance.getClass().equals("A"))
    result = ((A) instance).inputMatcher(x,y);


But this is bad, since it would require me to create a case for each and every subclass of C, in which I down-cast instance to that type.

The code I really want is something like:
result = downcast(instance, instance.getClass()).inputMatcher(a,b);
(will be screened)
(will be screened if not validated)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

February 2020

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags