6 Replies Latest reply on Mar 17, 2009 10:19 AM by 752426

    Casting problem

    807588
      Hello,

      I have this code:
      public class A {}
      
      public class B extends A {}
      
      B b = new B();
      System.out.println("Class:"+b.getClass().getName()+" instance of A:"+(b instanceof A)+" instance of B:"+(b instanceof B));
      
      A a = (A)b;
      System.out.println("Class:"+a.getClass().getName()+" instance of A:"+(a instanceof A)+" instance of B:"+(a instanceof B));
      and got this output:
      Class:B instance of A:true instance of B:true
      Class:B instance of A:true instance of B:true

      Does anyone know what Ive missed here?

      thnx in advance,
      Dragan

      Edited by: Kesa on Mar 17, 2009 2:45 AM
        • 1. Re: Casting problem
          752426
          Does anyone know what Ive missed here?
          Nothing. Output results are as expected.
          • 2. Re: Casting problem
            r035198x
            The cast just makes the compiler happy. The runtime is clever enough to know that that object is a B.

            Run this
             class A {
                      void  print() {
                           System.out.println("A");
                      }
                 }
                
                 class B extends A {
                     void  print() {
                           System.out.println("B");
                      }
                      
                 }
            and call a.print() on a B cast into an A object.
            • 3. Re: Casting problem
              807588
              public class CastTest {
              
                   /**
                    * @param args
                    */
                   public static void main(String[] args) {
                        
                         
                        B b = new B();
                        b.print();
                        System.out.println("Class:"+b.getClass().getName()+" instance of A:"+(b instanceof A)+" instance of B:"+(b instanceof B));
                         
                        A a = (A)b;
                        a.print();
                        System.out.println("Class:"+a.getClass().getName()+" instance of A:"+(a instanceof A)+" instance of B:"+(a instanceof B));
              
                   }
              
              }
              result is:

              B
              Class:B instance of A:true instance of B:true
              B
              Class:B instance of A:true instance of B:true


              I think I should get here:
              B
              Class:B instance of A:true instance of B:true
              A
              Class:A instance of A:true instance of B:false

              Am I right?
              • 4. Re: Casting problem
                807588
                The output is correct u havent missed anything

                public class A {}

                public class B extends A {}

                B b = new B();

                b instanceof A returns true because (Inheritance)


                A a = (A)b;
                a instanceof B returns true because(Polymorphism)

                where as u cant create B b=new A() this is called uni directional nature of polymorphism
                • 5. Re: Casting problem
                  r035198x
                  >
                  Am I right?
                  Run it and see.
                  The explicit casting does not change the object. It changes the type of the object so the compiler knows what to accept as valid statements concerning that object. The object is still a B so the runtime will print B.
                  • 6. Re: Casting problem
                    752426
                    A a = (A)b;
                    By doing this, you are just assigning reference of Type A to instance of Class B(child). The underlying object is still the same which is an instance of Class B. And as a, b refers to an instance of B, (a instanceof B) (b instanceof B) will return true.