Hi Gunter

Since B extends A you cannot cast A to B unless the object casted is
originally indeed of type B.
...
A aParam = new A();
B bParam = new B();
C c = new C();

c.foo(bParam); // will work
c.foo(aParam); // class cast exception
...

If you really need to do something like that, you can either catch the
exception and handle it, or find out if the parameter is maybe of type B
and handle other types separately.

Thomas

Gunter D'Hondt wrote:

>I'm currently trying to use polymorphism but I'm always getting a 
>ClassCastException:
>
>--------------------------
>public class A {}
>public class B extends A {}
>public class C {
>        public void foo(A a) {
>                B b = new B();
>                b = (B) a;
>        }
>}
>--------------------------
>How am I doing wrong here?
>
>Regards,
>Gunter
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to