Downcasting in Java

sonxEr77

Expert Member
Joined
Oct 22, 2006
Messages
1,809
Reaction score
0
Location
around
I believe you can cast an Object class to any class in Java. Because Object is super, then this must be down casting ... But, why can't we down cast any other base class to its subclass?

i.e
Code:
class Mamal {
}

class Cat extends Mamal {
}

void downCast() {
    Mamal m = new Mamal();
    Cat c = (Cat) m; // class cast exception?
    c = (Cat) new Object(); //?????
}
 
why would you wanna do that cos it doesnt make much sense...

Manager extends Employee{}

in this case a manager is always an employee...but you cannot say every employee is a manager.

:erm:
 
Top
Sign up to the MyBroadband newsletter
X