java - Determine the super class reference used to call method in subclass -
i've been asked couple of times, & stumped on how , why 1 go it.
here's scenario:
interface inta { void somemethod(); } interface intb { void somemethod(); }
two simple interfaces; implemented below in simple class
class classab implements inta,intb { @override public void somemethod(){ //some logic } }
then instantiate & use so:
inta = new classab(); a.somemethod();
or through other interface:
intb b = new classab(); b.somemethod();
now question in implementation of somemethod()
, can find out if used inta
reference or intb
reference invoke somemethod()
. , accordingly, execute different logic.
i feel wrong question. don't understand why & how 1 that. interfaces point of view, has implementation meets criteria of function signature & that's matters. if inta
& intb
expect different results, ought implemented in 2 different classes.
i've come across scenario twice, hence why i'm asking. there way determine? there way in other languages c/c++
?
not can't (except complicated wizardry), you've said, there's no reason try it.
the scenario might come up, if you've accidentally given same method signatures 2 interfaces commonly used , may end being implemented in same class together, distinct functionality. that's design error on interfaces' part.
there's no clean way around it, , unclean way complicated nobody in right mind attempt it. correct course of action refactor either 1 of interfaces use different method signature. sign of bigger design problems if scenario has come twice already.
Comments
Post a Comment