@Gazzonyx said:
Strangely proud and embarrassed by this line of code from a test GUI I'm using. I like to cast at the minute.
It's ugly but functional. I should mention that PersonPanels extend ContactPanels. Still, ...
Sorry if my question is dumb, I've never been too good at virtual dispatch. The code looks like Java, where instance methods are always virtual. So, how does the casting into (PersonPanel) possibly change the behavior of that line?
If contactPanel is an instanceof PersonPanel, and PersonPanel overrides setContact of the base ContactPanel class, then the PersonPanel implementation of setContact will always be called, even if contactPanel is declared as a ContactPanel.
So:
ContactPanel contactPanel = new PersonPanel();
contactPanel.setContact(contact); // Will call PersonPanel's setContact
((PersonPanel)contactPanel).setContact(contact); // This will call the exact same method as above.
Could you elaborate on what makes your code behave differently? I know there's some black magic with "final" methods, but I always assumed that just made the compiler refuse to allow having a method of the same name in the subclass, rather than acting like non-virtual methods in, say, C++.