ArrayList and Casting

Discussion in 'Java' started by Marc Twain, Oct 6, 2003.

  1. Marc Twain

    Marc Twain Guest

    Could someone please look at the code below and explain me why it's
    possible to Cast an ArrayList Object into an Employee then still be
    able to call instanceof over the casted object and check if its
    original class was Manager?

    Shouldn't instanceof retreive 'Employee' for all object cast as
    Employee?

    TIA!!:



    ArrayList employeeArrayList = new ArrayList(10);

    employeeArrayList.add(new Employee("Bob Dole", 24000.0, 01, 03,
    2003));
    employeeArrayList.add(new Manager("G W Bush", 16000.0, 13, 9, 2003,
    2000));
    employeeArrayList.add(new Employee("Bill Clinto", 21000, 10, 07,
    2002));


    for (int i = 0; i < employeeArrayList.size(); i++) {
    Employee e = (Employee)employeeArrayList.get(i);
    if (e instanceof Manager) { // how could this work??
    Manager tempManager = (Manager)e;
    tempManager.setAnnualBonus(10000.0);
    }
    e.raiseSalary(10);
    System.out.println(e);
    }
     
    Marc Twain, Oct 6, 2003
    #1
    1. Advertising

  2. Marc Twain

    VisionSet Guest

    "Marc Twain" <> wrote in message
    news:...
    > Could someone please look at the code below and explain me why it's
    > possible to Cast an ArrayList Object into an Employee then still be
    > able to call instanceof over the casted object and check if its
    > original class was Manager?
    >
    > Shouldn't instanceof retreive 'Employee' for all object cast as
    > Employee?
    >
    > TIA!!


    Because casting does not change the objects class, only its type.
    If an Object was instantiated as a Manager it will always be a Manager.
    It has type Manager & Employee. If you cast it to Employee then it is more
    portable, since it can be used anywhere an Employee is required, this is the
    main reason for casting. A Manager upcast to an Employee, despite it being
    of class Manager will only be able to access the methods declared in
    Employee, though if any of those methods are overriden in Manager, then it
    is actually the code in Manager that will run.

    --
    Mike W
     
    VisionSet, Oct 6, 2003
    #2
    1. Advertising

  3. Marc Twain

    Bjorn Abelli Guest

    "Marc Twain" wrote ...

    > Shouldn't instanceof retreive 'Employee' for
    > all object cast as Employee?


    No, since "Bush" *still* is an instance of the class Manager.

    The "cast" only change the type of reference to it.

    With a shorter example:

    Employee e =
    new Manager("G W Bush", 16000.0, 13, 9, 2003, 2000));

    Here the reference to "Bush" is implicitly "cast" to fit into a variable of
    reference-type "Employee", though he still *is* a Manager (whatever we think
    about if he's suited for it or not... ;-)


    // Bjorn A
     
    Bjorn Abelli, Oct 6, 2003
    #3
  4. Marc Twain

    Roedy Green Guest

    On 6 Oct 2003 07:12:34 -0700, (Marc Twain)
    wrote or quoted :

    >Shouldn't instanceof retreive 'Employee' for all object cast as
    >Employee?


    An object can be an instance of BOTH Dalmatian and Dog, as well as
    Object.

    Instanceof is not the same as asking what class is it.

    See http://mindprod.com/jgloss/classforname.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 6, 2003
    #4
  5. Marc Twain

    Roedy Green Guest

    On 6 Oct 2003 07:12:34 -0700, (Marc Twain)
    wrote or quoted :

    >Shouldn't instanceof retreive 'Employee' for all object cast as
    >Employee?


    Casting does not change the object. Casting does not chop off
    fields, to create a superclass object or add blank ones to create a
    derived class object.

    There are no easy tools in Java to convert objects up and down the
    class hierarchy. In assembler they would be easy.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 6, 2003
    #5
  6. Marc Twain

    Marc Twain Guest

    Thank you all for your answers, it's much clearer to me now!

    Cheers,

    Mark
     
    Marc Twain, Oct 7, 2003
    #6
    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. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,743
    Natty Gur
    Aug 19, 2003
  2. =?Utf-8?B?Sm9l?=

    Casting ArrayList entries as different structures

    =?Utf-8?B?Sm9l?=, Nov 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,538
    cbDevelopment
    Dec 4, 2005
  3. Kaidi
    Replies:
    4
    Views:
    2,386
    Kaidi
    Jan 3, 2004
  4. xz
    Replies:
    16
    Views:
    2,379
  5. Philipp
    Replies:
    6
    Views:
    928
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page