1 2 Previous Next 28 Replies Latest reply: Aug 21, 2008 6:35 AM by 800308 RSS

    doubt in reference variable casting ...............

    843785
      +class Animal { }+
      +class Dog extends Animal { }+
      +class DogTest {+
      +public static void main(String [] args) {+
      Animal animal = new Animal();
      Dog d = (Dog) animal;
      +}+
      +}+

      if we compile above code it works fine but at runtime it is showing "java.lang.ClassCastException".
      eventhough i did casting from super class ref var to sub class ref var i.e down casting.
      why this exception is coming and what is the solution for this one?
        1 2 Previous Next