[3720] in java-interest
Re: Class Casting and Dynamic Loading.
daemon@ATHENA.MIT.EDU (Gary Aitken)
Thu Nov 23 03:19:52 1995
Date: Wed, 22 Nov 1995 23:50:38 -0700
From: garya@village.org (Gary Aitken)
To: Bindu Wavell (RUFUS) <wavell@taussky.cs.colorado.edu>
Cc: java-interest@java.sun.com
In-Reply-To: <199511212007.NAA01632@taussky.cs.colorado.edu>
>Here is my problem: CNDemo creates an object, which it thinks is a
>CNDataStructure, but that is actually _descended_ from CNDataStructure.
>When this is passed to the descendant of CNAlgorithm, it does not seam
>to be possible to cast it back to a Descendant of CNDataStrucutre, so I
>can't access the data-structure specific methods.
If I understand your question correctly, the following example does
what you are trying to do:
class Base {
void narrow( Base b ) {
if ( b instanceof Sub ) {
Sub s; // b narrowed to Sub
s = (Sub) b;
s.f();
}
}
}
class Sub extends Base {
void f() { System.out.println( "Sub.f" ); }
}
public class NarrowTest {
public static void main( String argv[] ) {
Base b = new Base();
Sub s = new Sub();
b.narrow(s);
};
}
Gary Aitken garya@village.org
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com