1 2 Previous Next 28 Replies Latest reply: Aug 21, 2008 6:35 AM by 800308 Go to original post RSS
      • 15. Re: doubt in reference variable casting ...............
        843785
        MissAlexa wrote:
        If you remember, at the beginning of learning something, we're supposed to do lots of non-sense stuff, just to see how the compiler reacts. The scope is to learn.
        Right. And you've learnt that you can't cast an object to a type which it is not. As I said, an Animal is not a Dog, ergo, you can't cast Animal to Dog
        • 16. Re: doubt in reference variable casting ...............
          843785
          ganeshkumarch wrote:
          i want to call method which is there in subclass by using super class reference variable.....
          Please stick to one username. Pretending to be a girl won't get you any more help, really
          • 17. Re: doubt in reference variable casting ...............
            800308
            Pretending to be a girl won't get you any more help...
            Are you from Sydney or something? ;-)
            • 18. Re: doubt in reference variable casting ...............
              800604
              I think I understand...This guy should use an Array of animals.
              • 19. Re: doubt in reference variable casting ...............
                843785
                MissAlexa wrote:
                I think I understand...This guy should use an Array of animals.
                Well, I don't know how you arrive at that conclusion, but I've tried several times to explain what you're doing wrong to no avail, and you're playing sock puppets too, so, lots of luck
                • 20. Re: doubt in reference variable casting ...............
                  843785
                  ganeshkumarch wrote:
                  Animal animal = new Animal();
                  Dog d = (Dog) animal;
                  Your mistake is to think that the (Dog) cast will convert an Animal object to a Dog object. A cast can never change an object. An object is was it was when it it was created and that cannot be changed. This will work,
                  Animal animal = new Dog(); // upcast to Animal
                  Dog d = (Dog) animal; // downcast to Dog
                  The upcast (widening reference conversion) works becase the Dog object is also an Animal object. The compiler can check that at compiletime.

                  The downcast (narrowing refernce conversion) cannot be checked at compiletime so the compiler has to rely on your word. It does so by demanding that you explicitly put in a (Dog) cast. If you lied and the animal variable in fact didn't hold a reference to a Dog object you get caught at runtime with an exception.

                  Summary: A cast can never change an object into something it isn't already.
                  • 21. Re: doubt in reference variable casting ...............
                    800308
                    Summary: A cast never changes the type of the object at all... it effects the type of the reference to the object... A dog is a dog is a dog.
                    • 22. Re: doubt in reference variable casting ...............
                      800604
                      But you can say :
                      Animal aDog = new Dog();
                      And that is legal, because Dog extends Animal, so you can create a reference of type Animal, that points to a Dog. And that is polymorphism.
                      • 23. Re: doubt in reference variable casting ...............
                        843785
                        MissAlexa wrote:
                        But you can say :
                        Animal aDog = new Dog();
                        And that is legal, because Dog extends Animal, so you can create a reference of type Animal, that points to a Dog.
                        Check reply 20 one more time!
                        • 24. Re: doubt in reference variable casting ...............
                          800604
                          Oh, you're right...that's already been said. Sorry!
                          • 25. Re: doubt in reference variable casting ...............
                            800308
                            MissAlexa wrote:
                            Oh, you're right...that's already been said. Sorry!
                            Yep and the object is still a Dog!!!! even if the only reference you have to it is of the more generic Animal type...

                            Casting effects the reference type, not the object type (which is immutable). Clear?
                            • 26. Re: doubt in reference variable casting ...............
                              3004
                              uj_ wrote:
                              Animal animal = new Dog(); // upcast to Animal
                              Dog d = (Dog) animal; // downcast to Dog
                              The downcast (narrowing refernce conversion) cannot be checked at compiletime so the compiler has to rely on your word.
                              Just to add a bit of detail for the OP and head of arguments of the form, "But what about...", that is true in this particular case, but not in all cases.

                              There are cases where the compiler knows that the downcast cannot succeed. For example:
                              String s = "Dog"; 
                              Dog d = (Dog) s;
                              Since Dog is not a subtype of String, the compiler knows that cast cannot succeed.

                              In the discussion so far, and in UJ's example, Dog is a subtype of Animal, so it's possible that a reference of type Animal might point to a Dog object at rutime. This is what the compiler can't determine, and why the cast is allowed at compile time. If the Animal reference doesn't point to a Dog object at runtime, you get the ClassCastException.

                              In the String case, however, it's known at compile time that no String reference can ever point to a Dog object, so it's a compile-time error.
                              • 27. Re: doubt in reference variable casting ...............
                                843785
                                jverd wrote:
                                String s = "Dog"; 
                                Dog d = (Dog) s;
                                Since Dog is not a subtype of String, the compiler knows that cast cannot succeed.

                                In the discussion so far, and in UJ's example, Dog is a subtype of Animal, so it's possible that a reference of type Animal might point to a Dog object at rutime. This is what the compiler can't determine, and why the cast is allowed at compile time. If the Animal reference doesn't point to a Dog object at runtime, you get the ClassCastException.

                                In the String case, however, it's known at compile time that no String reference can ever point to a Dog object, so it's a compile-time error.
                                This can also be expressed in another way. The only time you can cast a reference is when you perform a narrowing or widening reference conversion, and those cases are defined here,

                                [http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.5]

                                And according to the rules for a narrowing conversion you can cast down from any interface to any class (that's not generic and not final) so this should be allowed at compiletime,
                                CharSequence s = "Dog"; // upcast from String to the CharSequence interface
                                Dog d = (Dog) s; // downcast to Dog
                                • 28. Re: doubt in reference variable casting ...............
                                  843785
                                  uj_ wrote:
                                  CharSequence s = "Dog"; // upcast from String to the CharSequence interface
                                  Dog d = (Dog) s; // downcast to Dog
                                  The Sun Salutation includes a Downward Dog.
                                  1 2 Previous Next