I've recently began the construction of a class, the mechanics of which have lead me to the conclusion that I don't know my way around
polymorphism 100%.
Here's what I'm doing:
The class contains a function which deals with a 'Targetable' argument - where 'Targetable' is an interface. The function then must deal with this 'Targetable' object differently depending upon whether this object is in actuality an Item object, a Player object, a Room object, or anything else that implements Targetable. A great many classes do.
What are my options here? So far I've come up with the following ideas, some of which I think belie my misunderstanding of polymorphism:
A) Function takes the argument as Object, then goes through a series of if(object.getClass().equals("Item")) statements.
B) Function takes the argument as Targetable, and Targetable has a kind of identifyYourself() function which all 'Targetable classes' have to implement, revealing their actual class.
Both seem a bit clunky to me at first glance. The idea that an object has to have an identifyYourself() function solely for the purpose of some coding minutiae seems unintuitive.
Specifics aside, my question is actually a general one: How does one take advantage of polymorphism by passing more abstract objects, possibly even passing Object itself, and still be able to recognize that object A needs to be dealt with slightly different than object B?
They are, after all, entirely different things which just happen to both be targetable by the player.