Originally posted by Tony Smith:
Parent p = new Parent();
Child c = new Child();
c = (Child)p;
Note that your code, as written, is not valid. We know that the run-time type of p is Parent, not Child (because we can see that you instantiated it that way). Casting is appropriate where the run-time type is
actually a child, and it is not, here.
Also, note that you are creating a new Child object and assigning it to c, only to be overwritten in the next line. Are you sure you understand the difference between defining a variable, and initializing it?
- Adam