You can't cast upwards i.e. there is no
way to cast A to a child B. Class A doesn't
know anything about B.
It works other way round. It is possible to
cast B downwards to A, because B includes
the information of A.
Jari
> -----Original Message-----
> From: EXT SABYASACHI S GUPTA
> [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, November 03, 1999 11:41 AM
> To: [EMAIL PROTECTED]
> Subject: Explicit type casting.
>
>
>
> I have 3 files to test this.
>
> //A.java
> public class A {
> public void method(){
> System.out.println("In A");
> }
> }
>
>
> //B.java
> public class B extends A {
>
> public void method()
> {
> System.out.println("hello world");
> }
> }
>
> //mymain.java
>
> import A;
> import B;
>
> public class mymain{
>
> public static void main(String args[])
> {
> B b=new B();
> A a=new A();
>
> b = (B)a;
>
> b.method();
> }
> }
>
> Since this requires an explicit cast I did just that
> But I get a runtime error
>
> Exception in thread "main" java.lang.ClassCastException: A
> at mymain.main(Compiled Code)
>
> Any help?????
>
>
> ----------------------------------------------------------------------
> To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
>
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]