Results 1 to 4 of 4
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Object is a Subclass of

    Say I have a class Entity

    Entity

    I have some subclasses of that, such as

    Tower
    Ship
    Projectile

    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)))" ?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by AndrewM16921 View Post
    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)))" ?
    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.

    kind regards,

    Jos

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    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...:)

Similar Threads

  1. Subclass name to be a parameter?
    By Peetahzee in forum New To Java
    Replies: 6
    Last Post: 12-12-2009, 03:51 PM
  2. subclass troubles
    By xf021209 in forum New To Java
    Replies: 12
    Last Post: 04-20-2009, 11:46 PM
  3. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM
  4. Replies: 1
    Last Post: 07-22-2008, 04:27 PM
  5. Subclass definition
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •