6 Replies Latest reply: Dec 6, 2007 6:26 PM by 807601 RSS

    casting parent class to a child class

    807601
      Hi,

      I have a static method which returns a class called parent. Now I want to cast it it's child class. There are no compilation errors, but at runtime it's throwing me ClassCastException. This is what I'm doing.
      public Parent getObject() {
      Parent p = new Parent();
      return p;
      }
      
      Child c = (Child) getObject();
      What is wrong with this code? I couldn't figure it out.
      Any help will be appreciated.

      Thanks,
        • 1. Re: casting parent class to a child class
          807601
          You can cast a parent reference to a child reference, but if the object that reference points to isn't an instance of the child class, you'll get ClassCastException.

          Casting doesn't change objects. It just says that a reference that was declared to be of one type should actually be treated as a reference to another type.
          • 2. Re: casting parent class to a child class
            807601
            Because you cannot do that.

            Parent is-a Child is false but
            Child is-a Parent is true.

            So you can cast a Child to a Parent but you cannot cast a Parent to a Child.
            • 3. Re: casting parent class to a child class
              807601
              Parent p1 = new Parent(); // 1
              Parent p2 = new Child(); // 2
              Child c1 = new Parent(); // 3
              Child c2 = new Child(); // 4
              
              Parent p1 = (Parent)c1; // 5
              Parnet p2 = (Parent)c2; // 6
              
              Child c3 = (Child)p1; // 7
              Child c4 = (Child)p2; // 8
              1: Ok.
              2: Ok. Every Child IS-A parent.
              3: Compile time error. A Parent object is not a Child.
              4: Ok.

              5: Ok, because every Child IS-A parent, and the cast is unnecessary. (Though since 3 is illegal a compile time we wouldn't actually have this situation.)
              6: Ok, and the cast is unnecessary, because every Child IS-A Parent.

              7: ClassCastException at runtime. The object is not a Child.
              8: Ok.
              • 4. Re: casting parent class to a child class
                807601
                Quacked wrote:
                So you can cast a Child to a Parent but you cannot cast a Parent to a Child.
                You can cast a Parent reference to Child IF the object it points to is a Child object (or if the reference is null).
                • 5. Re: casting parent class to a child class
                  807601
                  vanilla_lorax wrote:
                  Quacked wrote:
                  So you can cast a Child to a Parent but you cannot cast a Parent to a Child.
                  You can cast a Parent reference to Child IF the object it points to is a Child object (or if the reference is null).
                  Yeah I realised after I posted my answer was only directed against posted code and did not give a complete answer.
                  • 6. Re: casting parent class to a child class
                    807601
                    Thanks vanilla_lorax . I missed that point.