Downcasting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • snowfall
    New Member
    • Aug 2007
    • 56

    Downcasting

    What is downcasting and how to implement it??

    Am not getting any tutorial on this...

    pls help...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by snowfall
    What is downcasting and how to implement it??

    Am not getting any tutorial on this...

    pls help...
    Downcasting is considering an object to be an instantiation of a subclass, i.e.
    farther away from the root. Upcasting is considering an object an instantiation
    of a parent class, i.e. more towards the root class (which is the Object class).
    Here is an example:

    [code=java]
    class Base { }
    clase Derived extends Base { }
    ...
    Base b= new Derived();
    Derived d= new Derived);
    Base c= new Base();
    ...
    Derived d= (Derived)b; // ok downcast
    Derived e= (Derived)c; // fail downcast

    Base f= (Base)d; // always ok upcast
    Base g= (Base)c; // always ok useless upcast
    [/code]

    kind regards,

    Jos

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Originally posted by JosAH
      Downcasting is considering an object to be an instantiation of a subclass, i.e.
      farther away from the root. Upcasting is considering an object an instantiation
      of a parent class, i.e. more towards the root class (which is the Object class).
      Here is an example:

      [code=java]
      class Base { }
      clase Derived extends Base { }
      ...
      Base b= new Derived();
      Derived d= new Derived);
      Base c= new Base();
      ...
      Derived d= (Derived)b; // ok downcast
      Derived e= (Derived)c; // fail downcast

      Base f= (Base)d; // always ok upcast
      Base g= (Base)c; // always ok useless upcast
      [/code]

      kind regards,

      Jos
      This is new to me...

      Jos, about line number 5, did you forgot the left parenthesis? or it is an example of fail downcasting?!!

      Im confused..
      Sukatoa

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by sukatoa
        This is new to me...

        Jos, about line number 5, did you forgot the left parenthesis? or it is an example of fail downcasting?!!

        Im confused..
        Sukatoa
        Nope, it was simply a typo of mine; there should be a left parenthesis, sorry
        about that.

        kind regards,

        Jos

        Comment

        • snowfall
          New Member
          • Aug 2007
          • 56

          #5
          Originally posted by JosAH
          Downcasting is considering an object to be an instantiation of a subclass, i.e.
          farther away from the root. Upcasting is considering an object an instantiation
          of a parent class, i.e. more towards the root class (which is the Object class).
          Here is an example:

          [code=java]
          class Base { }
          clase Derived extends Base { }
          ...
          Base b= new Derived();
          Derived d= new Derived);
          Base c= new Base();
          ...
          Derived d= (Derived)b; // ok downcast
          Derived e= (Derived)c; // fail downcast

          Base f= (Base)d; // always ok upcast
          Base g= (Base)c; // always ok useless upcast
          [/code]

          kind regards,

          Jos
          Hi Jos, Thanks for the reply..

          Can the same be done for interfaces. For example i have a Base Interface whose some methods the first class should implement and other methods the second class must implement.
          i.e.; all the interface methods should not be available to both the classes.
          Only access to particular methods should be given.

          Can this be done?? (is this also downcasting??

          (Sorry for delay in reply.. Was not able to check net due to some issue)

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by snowfall
            Hi Jos, Thanks for the reply..

            Can the same be done for interfaces. For example i have a Base Interface whose some methods the first class should implement and other methods the second class must implement.
            i.e.; all the interface methods should not be available to both the classes.
            Only access to particular methods should be given.

            Can this be done?? (is this also downcasting??

            (Sorry for delay in reply.. Was not able to check net due to some issue)
            I don't understand your question? Are both of these classes abstract (they don't
            implement all of the interface methods)? Or is one a subclass of the (abstract)
            parent class? But yes, up and down casting works the same for interfaces.

            kind regards,

            Jos

            Comment

            • snowfall
              New Member
              • Aug 2007
              • 56

              #7
              Originally posted by JosAH
              I don't understand your question? Are both of these classes abstract (they don't
              implement all of the interface methods)? Or is one a subclass of the (abstract)
              parent class? But yes, up and down casting works the same for interfaces.

              kind regards,

              Jos
              No both are concrete classes only.. And there is no inheritance relationship between the classes. Is it possible to implement only some methods of our interface without using abstract concept??

              Dont we do that with adaptors in JFC... Is there any technique like that for POJO classes??

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by snowfall
                No both are concrete classes only.. And there is no inheritance relationship between the classes. Is it possible to implement only some methods of our interface without using abstract concept??

                Dont we do that with adaptors in JFC... Is there any technique like that for POJO classes??
                If a class states that it implements an interface and it does not implement all
                the methods declared in the interface then the class must be declared abstract.

                Adaptors always implement all the methods from an interface; they're dummy
                implementations . A subclass thereof needs only override those methods that
                are needed for that class. For examples see the adapators in the JSE.

                kind regards,

                Jos

                Comment

                • snowfall
                  New Member
                  • Aug 2007
                  • 56

                  #9
                  Originally posted by JosAH
                  If a class states that it implements an interface and it does not implement all
                  the methods declared in the interface then the class must be declared abstract.

                  Adaptors always implement all the methods from an interface; they're dummy
                  implementations . A subclass thereof needs only override those methods that
                  are needed for that class. For examples see the adapators in the JSE.

                  kind regards,

                  Jos
                  oh k.. thnks jos... I went to an interview.. There they said that, interface can be partially implemented and the concept for that is know asdowncasting..

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by snowfall
                    oh k.. thnks jos... I went to an interview.. There they said that, interface can be partially implemented and the concept for that is know asdowncasting..
                    Huh? If you only partially implement an interface then that class is abstract by
                    definition; it has nothing to do with casting in any direction except towards a
                    loony bin ;-)

                    kind regards,

                    Jos

                    Comment

                    Working...