Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Accessing a parent class' methods

Accessing a parent class' methods

Scheduled Pinned Locked Moved C#
comgame-devquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anthony Mushrow
    wrote on last edited by
    #1

    Say I have three classes, each a child of the previous. Can I explicitly call each of a classes methods that have been overridden?

    class A
    {
    public A() { }
    protected virtual void MakeLunch()
    {
    //Make cheese sandwich
    }
    }
    .
    class B : A
    {
    public B() { }
    protected override void MakeLunch()
    {
    base.MakeLunch();
    //Grab a banana
    }
    }
    .
    class C : B
    {
    public C() { }
    protected override void MakeLunch()
    {
    A.MakeLunch();
    //Get a drink
    }
    }

    From class C, can I skip the banana and just get a cheese sandwich and a drink? Or do I need one of my five-a-day?

    My current favourite word is: Nipple!

    -SK Genius

    Game Programming articles start -here[^]-

    E R 2 Replies Last reply
    0
    • A Anthony Mushrow

      Say I have three classes, each a child of the previous. Can I explicitly call each of a classes methods that have been overridden?

      class A
      {
      public A() { }
      protected virtual void MakeLunch()
      {
      //Make cheese sandwich
      }
      }
      .
      class B : A
      {
      public B() { }
      protected override void MakeLunch()
      {
      base.MakeLunch();
      //Grab a banana
      }
      }
      .
      class C : B
      {
      public C() { }
      protected override void MakeLunch()
      {
      A.MakeLunch();
      //Get a drink
      }
      }

      From class C, can I skip the banana and just get a cheese sandwich and a drink? Or do I need one of my five-a-day?

      My current favourite word is: Nipple!

      -SK Genius

      Game Programming articles start -here[^]-

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      I wouldn't imagine you could since B overrides A C should not "know" about A. However, if B used new on MakeLunch instead then it would be possible by casting C as A.

      Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
      Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
      Most of this sig is for Google, not ego.

      A N 2 Replies Last reply
      0
      • E Ennis Ray Lynch Jr

        I wouldn't imagine you could since B overrides A C should not "know" about A. However, if B used new on MakeLunch instead then it would be possible by casting C as A.

        Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
        Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
        Most of this sig is for Google, not ego.

        A Offline
        A Offline
        Anthony Mushrow
        wrote on last edited by
        #3

        I don't want to do that, it would cause more complications than it would solve. Thanks anyway.

        My current favourite word is: Nipple!

        -SK Genius

        Game Programming articles start -here[^]-

        E 1 Reply Last reply
        0
        • A Anthony Mushrow

          I don't want to do that, it would cause more complications than it would solve. Thanks anyway.

          My current favourite word is: Nipple!

          -SK Genius

          Game Programming articles start -here[^]-

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          I figured which is why I initially hesitated in responding. However, I think the reasoning is why such an action is prevented by the compiler.

          Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
          Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
          Most of this sig is for Google, not ego.

          1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            I wouldn't imagine you could since B overrides A C should not "know" about A. However, if B used new on MakeLunch instead then it would be possible by casting C as A.

            Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
            Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
            Most of this sig is for Google, not ego.

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            Ennis Ray Lynch, Jr. wrote:

            However, if B used new on MakeLunch instead then it would be possible by casting C as A.

            But that won't work too since the MakeLunch is marked as protected. Please correct me if I am wrong.

            Navaneeth How to use google | Ask smart questions

            E 1 Reply Last reply
            0
            • A Anthony Mushrow

              Say I have three classes, each a child of the previous. Can I explicitly call each of a classes methods that have been overridden?

              class A
              {
              public A() { }
              protected virtual void MakeLunch()
              {
              //Make cheese sandwich
              }
              }
              .
              class B : A
              {
              public B() { }
              protected override void MakeLunch()
              {
              base.MakeLunch();
              //Grab a banana
              }
              }
              .
              class C : B
              {
              public C() { }
              protected override void MakeLunch()
              {
              A.MakeLunch();
              //Get a drink
              }
              }

              From class C, can I skip the banana and just get a cheese sandwich and a drink? Or do I need one of my five-a-day?

              My current favourite word is: Nipple!

              -SK Genius

              Game Programming articles start -here[^]-

              R Offline
              R Offline
              Roger Alsing 0
              wrote on last edited by
              #6

              It does not work in C# , but it is possible at IL level. So I guess its simply a rule that prevents you to skip layers of abstraction in C#.

              My Blog

              1 Reply Last reply
              0
              • N N a v a n e e t h

                Ennis Ray Lynch, Jr. wrote:

                However, if B used new on MakeLunch instead then it would be possible by casting C as A.

                But that won't work too since the MakeLunch is marked as protected. Please correct me if I am wrong.

                Navaneeth How to use google | Ask smart questions

                E Offline
                E Offline
                Ennis Ray Lynch Jr
                wrote on last edited by
                #7

                I haven't tried it but it might, since C would still be in the inheritance chain for A

                Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
                Most of this sig is for Google, not ego.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups