Run base class's version of method in polymorphism

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • warren

    Run base class's version of method in polymorphism

    Hi,

    How do I call the base class's version of method when there is polymorphism?

    also, what if there are multiple levels of inheritance and i want to call
    the method that is
    several levels up?

    Thanks.


  • warren

    #2
    Re: Run base class's version of method in polymorphism

    sorry. give a simple example below:

    class A{
    public virtual void Method(){
    }
    }

    class B : A {
    public override void Method(){
    }
    }

    class C : B {
    public override void Method(){
    }
    }

    class Test{
    static void Main(string[] args ){

    C c = new C();

    A a = c;
    B b = c;

    // if i use a.Method() or b.Method(), they will call class C's
    version coz of polymorphism.
    // so how do i call Method() of class A and B?
    }
    }


    Comment

    • Cybertof

      #3
      Re: Run base class's version of method in polymorphism

      base.YourMethod ();

      For further levels, maybe this syntax should work :

      base.base.base. ...base.YourMet hod();


      Regards,
      Christophe.

      In article <3f82c079$1@new s.starhub.net.s g>, warren22432@yah oo.com
      says...[color=blue]
      > Hi,
      >
      > How do I call the base class's version of method when there is polymorphism?
      >
      > also, what if there are multiple levels of inheritance and i want to call
      > the method that is
      > several levels up?
      >
      > Thanks.
      >
      >
      >[/color]

      Comment

      • warren

        #4
        Re: Run base class's version of method in polymorphism


        "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
        news:MPG.19ece2 1949cb387998968 9@msnews.micros oft.com...[color=blue]
        > base.YourMethod ();[/color]

        I access it in another class which has the Main, not within the subclass
        itself.

        [color=blue]
        > For further levels, maybe this syntax should work :
        >
        > base.base.base. ...base.YourMet hod();[/color]


        no. i know this doesn't work.


        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: Run base class's version of method in polymorphism

          Warren,
          As Cybertof stated, you can use base to call your immediate parents method.

          To call grandparents or higher you will need to use reflection, I do not
          have a sample of using reflection handy.

          Hope this helps
          Jay

          "warren" <warren22432@ya hoo.com> wrote in message
          news:3f82c079$1 @news.starhub.n et.sg...[color=blue]
          > Hi,
          >
          > How do I call the base class's version of method when there is[/color]
          polymorphism?[color=blue]
          >
          > also, what if there are multiple levels of inheritance and i want to call
          > the method that is
          > several levels up?
          >
          > Thanks.
          >
          >[/color]


          Comment

          • Cybertof

            #6
            Re: Run base class's version of method in polymorphism

            Hum.....maybe i'm wrong but :

            If B derives from A, and C derives from B, then it means that
            A can be a particular B object
            or
            A can be a particular C object

            if A is a B object, then A.Method() will call B.Method()
            if A is a C object, then A.Method() will call C.Method()

            In your example, you make A beeing a C object and B beeing a C object
            too.
            So,
            A.Method() will call C.Method()
            B.Method() will call C.Method()

            Solution Hint :
            Maybe if you cast A to an A object during the call ?
            Or maybe if you box the A object into a real A type ?

            A RealA = A;
            RealA.Method() ?

            Tell me if it works or if they are compilation errors.....

            (I'm interested in knowing the answer too...)


            Regards,
            Christophe.



            In article <3f82c304$1@new s.starhub.net.s g>, warren22432@yah oo.com
            says...[color=blue]
            > sorry. give a simple example below:
            >
            > class A{
            > public virtual void Method(){
            > }
            > }
            >
            > class B : A {
            > public override void Method(){
            > }
            > }
            >
            > class C : B {
            > public override void Method(){
            > }
            > }
            >
            > class Test{
            > static void Main(string[] args ){
            >
            > C c = new C();
            >
            > A a = c;
            > B b = c;
            >
            > // if i use a.Method() or b.Method(), they will call class C's
            > version coz of polymorphism.
            > // so how do i call Method() of class A and B?
            > }
            > }
            >
            >
            >[/color]

            Comment

            • warren

              #7
              Re: Run base class's version of method in polymorphism


              "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message
              news:%231idAsNj DHA.4064@TK2MSF TNGP11.phx.gbl. ..[color=blue]
              > Warren,
              > As Cybertof stated, you can use base to call your immediate parents[/color]
              method.

              if u see my simple example, i can't use the keyword base in the class Test.

              [color=blue]
              > To call grandparents or higher you will need to use reflection, I do not
              > have a sample of using reflection handy.
              >[/color]

              if so, then it is much more complex than what i thought. :(


              Comment

              • warren

                #8
                Re: Run base class's version of method in polymorphism


                "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
                news:MPG.19ece5 948adcf8ac98968 b@msnews.micros oft.com...[color=blue]
                > Hum.....maybe i'm wrong but :
                >
                > If B derives from A, and C derives from B, then it means that
                > A can be a particular B object
                > or
                > A can be a particular C object
                >
                > if A is a B object, then A.Method() will call B.Method()
                > if A is a C object, then A.Method() will call C.Method()
                >
                > In your example, you make A beeing a C object and B beeing a C object
                > too.
                > So,
                > A.Method() will call C.Method()
                > B.Method() will call C.Method()[/color]


                Ya. That is the point of polymorphism. ;)

                [color=blue]
                > Solution Hint :
                > Maybe if you cast A to an A object during the call ?
                > Or maybe if you box the A object into a real A type ?
                >
                > A RealA = A;
                > RealA.Method() ?
                >
                > Tell me if it works or if they are compilation errors.....
                >
                > (I'm interested in knowing the answer too...)[/color]

                doesn't work after i tried. u don't need to do explicit cast,
                because c 'is' an A. So explicit cast does the same as an implicit cast.



                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Run base class's version of method in polymorphism

                  warren <warren22432@ya hoo.com> wrote:[color=blue][color=green]
                  > > base.YourMethod ();[/color]
                  >
                  > I access it in another class which has the Main, not within the subclass
                  > itself.[/color]

                  In that case, you can't do it - and very deliberately so, as it would
                  break encapsulation.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Run base class's version of method in polymorphism

                    Cybertof <cybertof2003no spam@gmx.net> wrote:

                    <snip>
                    [color=blue]
                    > Solution Hint :
                    > Maybe if you cast A to an A object during the call ?[/color]

                    Nope - precisely because the call is polymorphic.
                    [color=blue]
                    > Or maybe if you box the A object into a real A type ?
                    >
                    > A RealA = A;
                    > RealA.Method() ?[/color]

                    That's not boxing, and it wouldn't do the trick either.

                    This is quite important, IMO, as otherwise callers could bypass very
                    deliberate restrictions etc of derived classes.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: Run base class's version of method in polymorphism

                      Jay B. Harlow [MVP - Outlook] <Jay_Harlow@ema il.msn.com> wrote:[color=blue]
                      > To call grandparents or higher you will need to use reflection, I do not
                      > have a sample of using reflection handy.[/color]

                      Apart from a couple of strange exceptional cases (GetHashCode being an
                      example, I think) this won't work either - the reflection call is
                      virtual too.

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: Run base class's version of method in polymorphism

                        Jon,
                        I admit GetHashCode was the example I was thinking of.

                        I'll have to find my example, as I was thinking it would work for any
                        method.

                        I know VB.NET does something as its MyClass allows you to call the method
                        specific to that class, although there are derived classes, not quite the
                        same thing...

                        Although I agree with you, except for some very special exceptional cases
                        you should not be attempting to call a polymorphic function, in a specific
                        level of the hierarchy.

                        Thanks
                        Jay

                        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                        news:MPG.19ecda 3094dcfa4a98981 6@msnews.micros oft.com...[color=blue]
                        > Jay B. Harlow [MVP - Outlook] <Jay_Harlow@ema il.msn.com> wrote:[color=green]
                        > > To call grandparents or higher you will need to use reflection, I do not
                        > > have a sample of using reflection handy.[/color]
                        >
                        > Apart from a couple of strange exceptional cases (GetHashCode being an
                        > example, I think) this won't work either - the reflection call is
                        > virtual too.
                        >
                        > --
                        > Jon Skeet - <skeet@pobox.co m>
                        > http://www.pobox.com/~skeet
                        > If replying to the group, please do not mail me too[/color]


                        Comment

                        • 100

                          #13
                          Re: Run base class's version of method in polymorphism

                          Hi warren,
                          If you need to do that you don't need polymorphism. Just declare the methods
                          as non-virtual and you can call whatever method you want just by casting the
                          reference to type which method you want to call.
                          Anyway if you do need this you have to make it youirown. This is my
                          solution:

                          //----------------- Class A --------------------
                          class A
                          {
                          private string mName = "Class A";

                          public A RealA
                          {
                          get{return new A(this);}
                          }

                          protected A(A source)
                          {
                          //initlizes from the source
                          mName = source.mName;
                          }

                          public A()
                          {
                          }

                          public virtual void Foo()
                          {
                          Console.WriteLi ne("A");
                          }
                          }
                          //------------------------------------------
                          class B: A
                          {
                          private string mName = "Class B";

                          public B RealB
                          {
                          get{return new B(this);}
                          }

                          protected B(B source):base(so urce)
                          {
                          //initlizes from the source
                          mName = source.mName;

                          }

                          public B()
                          {
                          }

                          public override void Foo()
                          {
                          Console.WriteLi ne("B");
                          }

                          }

                          So now you can have

                          B b = new B();
                          A a = b;

                          b.Foo(); //Calls B.Foo()
                          b.RealA.Foo(); //Calls A.Foo()

                          a.Foo(); //Calls B.Foo
                          a.RealA.Foo(); //Calls A.Foo();

                          However, if we had had class C built following the same model we would have
                          called its method like:

                          C c = new C();
                          A a = c;

                          c.Foo(); //Calls C.Foo()
                          c.RealB.Foo(); //Calls B.Foo()
                          c.RealA.Foo(); //Calls A.Foo();


                          a.Foo(); //Calls C.Foo()
                          a.RealA.Foo(); //Calls A.Foo()
                          ((B)a ).RealB.Foo(); //Calls B.Foo()
                          ((C)a ).RealB.Foo(); //Calls B.Foo()

                          Got the picture?

                          HTH
                          B\rgds
                          100



                          Comment

                          Working...