ArrayList and Casting

M

Marc Twain

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);
}
 
V

VisionSet

Marc Twain said:
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.
 
B

Bjorn Abelli

...
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
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top