2 Replies Latest reply: Feb 19, 2007 12:04 PM by 807599 RSS

    inheritance and object casting

    807599
      i have a question on inheritance and object casting
      suppose

      class abc
      {
      int a,b,c
      public void display()
      {...............
      }
      }
      class abcd extends abc
      {
      int d
      public void display()
      {...............
      }
      }
      class abcde extends abcd
      {
      int e
      public void display()
      {.............
      }
      }

      there are three classes
      (base)class abc->(child)class abcd->(grandchild)class abcde

      their objects, respective classes and variables are as follows
      (object) obj1->(class) class abc->(member varialbes) a,b,c
      (object) obj2->(class) class abcd->(member variables)a,b,c,d
      (object) obj2->(class) class abcde->(member variables)a,b,c,d,e

      all the three classes have public void display() method with the same signature

      can the grandchild object invoke display() method in base class without invoking the display() method in the child class i.e its immediate base class

      also in object casting
      do we lose or gain precision as in normal type casting
      for eg.
      float f=1.234;
      converted to int
      int i=(int)f;
      would give i=1
      losing the 0.234 precision

      now in the above three class example
      what happens when base class object is converted into derived class object
      and what happens when we convert derived classs object into a base class object
      and the base class object conversion into grandchild class object and viceversa

      ie. if grandchild class object is converted into base class object
      do we loose the value stored in c and d only or completly new object is created

      regards

      invis_eye_mage
        • 1. Re: inheritance and object casting
          3004
          can the grandchild object invoke display() method in
          base class without invoking the display() method in
          the child class i.e its immediate base class
          No. The child can explicitly call its immediate parent's methods with super.method() but it can't invoke its more distant ancestors' methods.


          also in object casting
          do we lose or gain precision as in normal type
          casting
          Neither. Precision is not an issue for objects.


          now in the above three class example
          what happens when
          Try it and see for yourself.


          base class object is converted into
          derived class object
          You cannot "convert" one object to another class by casting. All casting does is tell the compiler and the runtime to treat a reference of one type as a reference of another type. It does not affect the object at all.

          and what happens when we convert derived classs
          object into a base class object
          Again: 1) you don't convert objects. 2) try it and see.
          • 2. Re: inheritance and object casting
            807599
            nevermind =)

            #