1 person found this helpful
I have two objects that i need to iterate, they both have the same method i need to call during iteration, but those two objects are from different libraries and i cannot change them to add them as implement interface...
You already know what you need to do. You just don't want to do it.
You can't treat two (or more) instances the same if they aren't the same. Here are three methods:
1. Add code (like you propose) to determine which type you have
2. Create your own classes that extend those classes and have your own class implement your own interface that has the 'getName' method. When you create instances of your own class the constructor can have code that determines which type you have and then use the appropriate 'getName' method. Your app code would use your own classes instead of the ones from the libraries
3. Use reflection to call the 'getName' method.