Downcasting

class A

{

void foo()

{

System.out.println("B");

}

}

class Bextends A

{

}

class Demo

{

publicstaticvoid main(String args[])

{

B b = (B)(new A());

b.foo();

}

}

I get a Runtime Class-Cast Exception

#include<iostream>

using namespace std;

class A

{

public:

void foo()

{

cout<<"A";

}

};

class B:public A

{

};

int main()

{

B *b = (B*)(new A());

b->foo();

getchar();

return 0;

}

and this works in C++

so, i suspect downcasting is not supported in Java as opposed to C++?

[1821 byte] By [enterneoa] at [2007-11-27 8:38:09]
# 1

Hello, I can't explain why C++ allows this kind of behavior, but I can try explaining why Java doesn't. Consider, for example, that your code was:

class A

{

void foo()

{

System.out.println("foo");

}

}

class B extends A

{

void bar()

{

System.out.println("bar");

}

}

class Demo

{

public static void main(String args[])

{

B b = (B)(new A());

b.foo();

b.bar(); //What would this do?

}

}

Now, if you were to cast a new A object to B, you would be "allowed" to call "bar", even though your object is in-fact from the class A, meaning it doesn't have a "bar" method! You can say every B is in-fact also an A because they have a common interface, but you cannot say every A is a B because B extends A - it may have methods that A just doesn't have.

Hope this helped,

laginimaineb.

laginimaineba at 2007-7-12 20:35:42 > top of Java-index,Java Essentials,New To Java...
# 2
> so, i suspect downcasting is not supported in Java as> opposed to C++?so C++ gives you the ability to shoot yourself in the foot and lets call it a feature.
petes1234a at 2007-7-12 20:35:42 > top of Java-index,Java Essentials,New To Java...
# 3
Well.... If you're 100% sure you know what you're doing there may be something you can do with that... I just don't know what :PLaginimaineb.
laginimaineba at 2007-7-12 20:35:42 > top of Java-index,Java Essentials,New To Java...
# 4

> so, i suspect downcasting is not supported in Java as

> opposed to C++?

Downcasting is supported, but the object actually has to BE of the class you're casting to.

When you say it "works" in C++, you're not seeing the whole picture. Your B is no different from your A here, and you're not acessing any nonexistent field or method. If B were different, and you tried to access something that's defined on B but not A, something would break.

jverda at 2007-7-12 20:35:42 > top of Java-index,Java Essentials,New To Java...
# 5

#include<iostream>

using namespace std;

class A

{

public:

void foo()

{

cout<<"A";

}

};

class B: public A

{

};

int main()

{

A *a = new B();

B *b = (B*)(a);

b->foo();

getchar();

return 0;

}

I realize I shouldnt be posting C++ code here, but still just to complete the thread, i expect this is correct downcast ! , and the one i did before would case trouble

enterneoa at 2007-7-12 20:35:42 > top of Java-index,Java Essentials,New To Java...
# 6

and this is similar code in Java

class A

{

void foo()

{

System.out.println("B");

}

}

class B extends A {}

class Demo

{

public static void main(String args[])

{

A a = new B();

B b = (B)a;// Downcast!

b.foo();

}

}

Downcasting in not supported in Java, isnt this supported here?

enterneoa at 2007-7-12 20:35:42 > top of Java-index,Java Essentials,New To Java...
# 7
but no object slicing occurs which I think happens in c++.
petes1234a at 2007-7-12 20:35:42 > top of Java-index,Java Essentials,New To Java...
# 8

> I realize I shouldnt be posting C++ code here, but

> still just to complete the thread, i expect this is

> correct downcast ! , and the one i did before would

> case trouble

You apparently either didn't read or didn't understand my previous post.

Java allows downcasting. You can cast a reference of a parent class to a reference of a child class, IFF the object pointed to by that reference is of the child class (or one of its descendants).

C++ is not as restrictive, BUT once you try to start accessing stuff in that child object that's not the same as the parent, you're going to be off in unpecified parts of memory doing who knows what but it won't be good.

jverda at 2007-7-12 20:35:42 > top of Java-index,Java Essentials,New To Java...