Link to home
Start Free TrialLog in
Avatar of anhben
anhben

asked on

why upcasting is dangerous

could you explain how upcasting using in java and why it is
dangerous to use it

thanks
Avatar of heyhey_
heyhey_

what do you mean be 'upcasting' ? but whatever it is, Java is a pretty safe language, so it can't be too dangerous ... :)
ASKER CERTIFIED SOLUTION
Avatar of BigRat
BigRat
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am sorry, but for me, what BigRat explains is downcasting.

From the Object-Orientation FAQ:

"Downcasting is the term used in C++ for casting a pointer or reference to a base class to a
derived class. "

Fred extends Object => (Fred)anObject is the action to cast a base class (Object) to
a derived class (Fred)  => downcasting.

Upcasting is exactly the opposite. Please consider the following piece of code:

http://jinx.umsl.edu/~subraman/cast0_ex1.html

It is a C++ example, but OO is OO. In short, if we have (B extends A) and  (C extends B)
we can cast the following ways:

        A a = new A(); // a is an instance of A
        B b = new B(); // b is an instance of B
        C c = new C(); // c is an instance of C

        B bb = (B)c; // we are upcasting C to B
        A aa = (A)c; // we are upcasting C to A
        C cc = (C)c; // we are downcasting B to C

The "problem" with upcasting is the following (we consider bb, which is an upcasted
version of c): the upcasted version has no more access to the functionalities (fields
and methods) that were provided by the original class. For example, if we have:

class A {
}

class B extends A {
}

class C extends B {

     public void print() {
         //...
     }
}

the upcasted version of c, which is now a B instance, has no more access to the print()
method.
OK The Rat accepts what fontaine proposes, but I do not see how it is dangerous and furthermore it's automatic in Java.
I agree, there is nothing dangerous about this (and I have verified what was written about
upcasting/downcasting in several locations). The only thing one has to be carefull about is
to remember what was the original class exactly. For example, when you stock an object in a
Vector, it is implicitely upcasted (as the method to add in a Vector is addElement(Object o)).
To recover the original class, you have to remember what was exactly the class of the object
stored in the Vector, in order to downcast it and be able to access the fields and methods
that are making the class what it is. Remember indeed that the nextElement() method of a Vector enumeration only returns an Object class...

I have found an explanation in Bjarne Stroustrup's C++ book about the upcasting/downcasting
terminology. The classical way to draw an inheritance diagram is to draw one box per
class, the base class above the derived classes with an arrow from the derived class box
to the base class box. When one casts a derived class to a base class, we are going up
in the graph, hence the term "upcasting". When one casts a base class to a derived
class, we are going down in the graphs, hence the term "downcasting".
>> The only thing one has to be carefull about is to remember what was the original class exactly.

but in Java you don't have to remember what was the original class of an object - every object has assotiated class and you can always get it using Object.getClass() method

java is real OO language - in Java you have Objects that livce thair own live - every object has a class - and variables - variable has classes too, and every variable can point to NULL, an Object of its class or some derived class.

so when you have this code
String st = "sample";
Object o = (Object)st;
String st2 = (String)o;

all the variables (st, st2, o) point to the same Object (of class String) but they have different variable types ...
so
Object o = (Object)st; // upcasting
is not dangerous at all - yopu stil have the same object, but you just use a different variable type that points to it


Looks like the person asking the question already forgot about it...