Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: Dealing with Subclasses

  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Dealing with Subclasses

    Hello,
    I have a project that I am working on and I want to create a method that can deal with an object differently based on what kinda of subclass it is. For example. I have the class Item and I have 2 classes which extend item, task and event respectively. I have an item manager which essentially just holds lists of all existing items. How can I create a method which determines what type of item it is dealing and then return that event or task back to the calling method?

    Thank you in advance!

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,619
    Rep Power
    5

    Default Re: Dealing with Subclasses

    What about using getClass()?

    Regards,
    Jim
    Last edited by jim829; 08-17-2013 at 10:26 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Re: Dealing with Subclasses

    Thanks for the fast reply, I will look into those. Let me see if I can explain my situation better first though. Say I have an incoming object and I dont know if it's gonna be an event or task, but they are both subclasses of item so I begin doing some things with it as an item. At one point I decide however that I need to determine if this object is an event or task and then process the object accordingly, how would I determine what kind of subclass the item is?

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,619
    Rep Power
    5

    Default Re: Dealing with Subclasses

    What have you tried? Why not set up some classes just like you described. You can also used instanceof to see what type of object it is and cast accordingly.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    radish is offline Member
    Join Date
    Aug 2013
    Posts
    16
    Rep Power
    0

    Default Re: Dealing with Subclasses

    I think you may want isinstance or instanceof...

    inheritance - Java isInstance vs instanceOf operator - Stack Overflow

    I believe I know this because I was watching some tutorials about Spring, a framework for doing such things...

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default Re: Dealing with Subclasses

    Quote Originally Posted by aianta View Post
    Thanks for the fast reply, I will look into those. Let me see if I can explain my situation better first though. Say I have an incoming object and I dont know if it's gonna be an event or task, but they are both subclasses of item so I begin doing some things with it as an item. At one point I decide however that I need to determine if this object is an event or task and then process the object accordingly, how would I determine what kind of subclass the item is?
    Why not let those tasks and events do that 'special' processing themselves? They know what they are:

    Java Code:
    public class Item {
       public void process() { /* nothing to do here */ }
    }
    public class Task {
       public void process() { /* task specific processing here */ }
    }
    public class Event {
       public void process() { /* event specific processing here */ }
    }
    The caller of that method couldn't care less what that specific processing is, as long as the object knows how to process(); which is true for all Items and sub classes.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Dealing with really big numbers
    By Zigster in forum New To Java
    Replies: 4
    Last Post: 04-26-2012, 09:46 AM
  2. Dealing with DatagramSocket
    By fabiolanza in forum Advanced Java
    Replies: 1
    Last Post: 04-07-2012, 02:14 AM
  3. Dealing with IndexOutOfBoundsExceptions
    By MrDave in forum New To Java
    Replies: 4
    Last Post: 06-19-2010, 01:33 AM
  4. dealing with (zip) files
    By aloula in forum Advanced Java
    Replies: 4
    Last Post: 10-06-2009, 07:32 PM
  5. Dealing with iReport 3.0.0
    By HotEvilGirl in forum New To Java
    Replies: 6
    Last Post: 09-11-2009, 11:32 AM

Tags for this Thread

Posting Permissions

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