Object is a Subclass of
Say I have a class Entity
I have some subclasses of that, such as
And, Tower for example, I have a number of subclasses. I'll just say A, B, C, etc.
So, if I pass an Entity to a function, I can tell if it's a tower by using "e instanceof Tower" ... but once I know it's a Tower, how can I tell if it was made as just Tower or if it's a subclass of Tower, like A B or C, without writing out "(!(e instanceof A) && !(e instanceof B) && !(e instanceof C)))" ?
you can call getClass() and then compare via the equals( ... ) method as this will only be true if once class is precisely the other class.
A question for you: why are you interested in a thing being an A, B or C or not? Isn't It enough that you/your code knows that it's dealing with a Tower? As a rule of thumb you shouldn't extend from a concrete class.
Originally Posted by AndrewM16921
Indeed, if you find yourself having to figure out what class an object you're working with is, then you probably (though not always) have a problem in your code.
Would it, for example, be more sensible to have an enumeration of TowerTypes? Each of which has certain methods implemented to handle the different rules each has?
Or have an abstract method for firing (or whatever it is you need to do), and each tower subclass has its own one?
Not knowing your requirements I'm throwing stuff out there...:)