How to tell which subclass we used?

Discussion in 'Java' started by Ook, Feb 18, 2007.

  1. Ook

    Ook Guest

    This is a textbook assignment: I have a class, Item. I have subclasses, CD
    and DVD.

    public class Item
    {...}

    public class DVD extends Item
    {...}

    public class CD extends Item
    {...}

    So when I create an instance of Item, I do this:

    Item cd = new CD();

    Question. When I iterate my Item collection, how can I tell whether an
    instance is CD or DVD? Is there a native way to do this, some property of
    Item, or do I need to create a property in Item that identifies it as a DVD
    or CD instance?
    Ook, Feb 18, 2007
    #1
    1. Advertising

  2. Ook

    Ook Guest

    This is a better question then the original. Given a class Item where CD and
    DVD are subclasses. I do this:

    public ArrayList<Item> List = new ArrayList<Item>();

    And I add several the following to my ArrayList, List, so I have an
    ArrayList of items of both CDs and DVDs.

    Item cd = new CD();
    Item dvd = new DVD();

    To get a list of CDs only, I do this:

    ArrayList<Item> list1 = new ArrayList<Item>();
    for( Item item: List)
    {
    if( item instanceof CD)
    list1.add( item );
    }
    return list1 ;

    This works. Question - is this the most effecient way to do this, is there a
    better way to return a list of Items of type CD only?
    Ook, Feb 18, 2007
    #2
    1. Advertising

  3. Ook

    Lew Guest

    Ook wrote:
    > This is a better question then the original. Given a class Item where CD and
    > DVD are subclasses. I do this:
    >
    > public ArrayList<Item> List = new ArrayList<Item>();
    >
    > And I add several the following to my ArrayList, List, so I have an
    > ArrayList of items of both CDs and DVDs.
    >
    > Item cd = new CD();
    > Item dvd = new DVD();
    >
    > To get a list of CDs only, I do this:
    >
    > ArrayList<Item> list1 = new ArrayList<Item>();
    > for( Item item: List)
    > {
    > if( item instanceof CD)
    > list1.add( item );
    > }
    > return list1 ;
    >
    > This works. Question - is this the most effecient way to do this, is there a
    > better way to return a list of Items of type CD only?


    For the suggested schema, wherein CD and DVD classes each inherit from Item,
    and ones like it, the trick is never to ask an Item which kind it is. In other
    words, the whole point of declaring a List or variable to hold type Item is
    that you no longer explicitly care which subclass is in there at runtime.

    If you need a List of CD, then declare it List<CD>. If you need a List of
    Items that have to all be of only one subtype, e.g., all CDs or all DVDs, but
    not a mix of both in the same list, declare it List<? extends Item>. If you
    want a list of Items, and you can mix or match which kind of items, then
    declare a List<Item>.

    Why do I say that you do not care which kind of item it is, and thus recommend
    that you never call "if ( item instanceof ...)"?

    Because you likely only want the Item to do something all Items can do, just
    differently. For example, suppose you wanted to know the maximum capacity in
    bytes of your CD or DVD, but didn't know which.

    Item item = getItemThatIsReallyOfASubtype();
    int capacity = item.getCapacity();

    Notice that this snippet does not need to know what subtype the item is
    instanceof. 'item' knows its own type, so the invoking code doesn't need to.

    Google "polymorphism" in object-oriented programming.

    Usually if you are explicitly asking an object its runtime type you have
    missed a better way to solve the actual problem. If you decide you need the
    enumeration of subtypes, and less likely, if you are correct in that
    assessment, you incur the maintenance surcharge of code changes every time the
    inheritance hierarchy changes. (Which for some applications is never.)

    Polymorphic approaches work even if someone else extends the class with a
    subtype the original supertype designer never thought of in an application the
    original supertype designer never thought of.

    Ask yourself what you need to do with the returned list or value in your
    snippet, and how you might solve that problem without explicit subtype
    knowledge in the invoking code.

    - Lew
    Lew, Feb 18, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    438
    jstorta
    Feb 20, 2006
  2. Frank Millman
    Replies:
    10
    Views:
    601
    Roger Binns
    May 4, 2004
  3. S.Volkov
    Replies:
    2
    Views:
    213
    S.Volkov
    Mar 12, 2006
  4. Trans
    Replies:
    8
    Views:
    320
    Robert Klemme
    Oct 23, 2008
  5. Fab

    Subclass of subclass

    Fab, Aug 9, 2012, in forum: C++
    Replies:
    0
    Views:
    394
Loading...

Share This Page