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. Inheritance

Inheritance

Scheduled Pinned Locked Moved C#
oopquestion
7 Posts 5 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.
  • S Offline
    S Offline
    sunsher
    wrote on last edited by
    #1

    Hi, I have a class A that inherits another class B and calls a method in Class B. That method in turn calls another method which is overridable in Class B. Now, I want the inner method to be created in the calling Class A. But I noticed that the control comes to the first method in Class B and then goes to the inner method in Class B itself. Is it possible to bypass that inner method and call my overrided method instead? Thanks

    P B R OriginalGriffO 4 Replies Last reply
    0
    • S sunsher

      Hi, I have a class A that inherits another class B and calls a method in Class B. That method in turn calls another method which is overridable in Class B. Now, I want the inner method to be created in the calling Class A. But I noticed that the control comes to the first method in Class B and then goes to the inner method in Class B itself. Is it possible to bypass that inner method and call my overrided method instead? Thanks

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Can you show the minimum amount of code required to demonstrate the situation?

      1 Reply Last reply
      0
      • S sunsher

        Hi, I have a class A that inherits another class B and calls a method in Class B. That method in turn calls another method which is overridable in Class B. Now, I want the inner method to be created in the calling Class A. But I noticed that the control comes to the first method in Class B and then goes to the inner method in Class B itself. Is it possible to bypass that inner method and call my overrided method instead? Thanks

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        The least you can do is show the code you are working with, and make some effort. Look up the words 'new and 'override in the context of .NET inheritance, think about them, try using them, then, come back here with specific questions and code.

        «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

        1 Reply Last reply
        0
        • S sunsher

          Hi, I have a class A that inherits another class B and calls a method in Class B. That method in turn calls another method which is overridable in Class B. Now, I want the inner method to be created in the calling Class A. But I noticed that the control comes to the first method in Class B and then goes to the inner method in Class B itself. Is it possible to bypass that inner method and call my overrided method instead? Thanks

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #4

          That's a confusing sentence, but make sure the methods in the base class are marked as virtual.

          Regards, Rob Philpott.

          1 Reply Last reply
          0
          • S sunsher

            Hi, I have a class A that inherits another class B and calls a method in Class B. That method in turn calls another method which is overridable in Class B. Now, I want the inner method to be created in the calling Class A. But I noticed that the control comes to the first method in Class B and then goes to the inner method in Class B itself. Is it possible to bypass that inner method and call my overrided method instead? Thanks

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            Yes - that is exactly what inheritance and method overriding is all about!

            public class A : B
            {
            public void DoIt()
            {
            Console.WriteLine("A:DoIt");
            InB();
            }
            public override void Inner()
            {
            base.Inner();
            Console.WriteLine("A:Inner");
            }
            }
            public class B
            {
            public void InB()
            {
            Console.WriteLine("B:InB");
            Inner();
            }
            public virtual void Inner()
            {
            Console.WriteLine("B:Innner");
            }
            }

            Then when you create an instance:

                    A a = new A();
                    a.DoIt();
            

            You get:

            A:DoIt
            B:InB
            B:Innner
            A:Inner

            Because B.Inner is marked as virtual it can be overridden in derived classed - as it is in A - and the system will look for the most "appropriate" version to call. Because a is declared as an instance of the A class, it calls the A version of the method. If you declared a as a B instead but assigned it an A (which is perfectly fine as every A is a "superset" of B):

                    B b = new A();
                    b.Inner();
            

            You can't call the DoIt method (because B doesn't contain a definition for DoIt), but you will still get:

            B:Innner
            A:Inner

            Becuase the value inside the variable b is still an instance of the A class so the system calls the "latest" version.

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            S 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Yes - that is exactly what inheritance and method overriding is all about!

              public class A : B
              {
              public void DoIt()
              {
              Console.WriteLine("A:DoIt");
              InB();
              }
              public override void Inner()
              {
              base.Inner();
              Console.WriteLine("A:Inner");
              }
              }
              public class B
              {
              public void InB()
              {
              Console.WriteLine("B:InB");
              Inner();
              }
              public virtual void Inner()
              {
              Console.WriteLine("B:Innner");
              }
              }

              Then when you create an instance:

                      A a = new A();
                      a.DoIt();
              

              You get:

              A:DoIt
              B:InB
              B:Innner
              A:Inner

              Because B.Inner is marked as virtual it can be overridden in derived classed - as it is in A - and the system will look for the most "appropriate" version to call. Because a is declared as an instance of the A class, it calls the A version of the method. If you declared a as a B instead but assigned it an A (which is perfectly fine as every A is a "superset" of B):

                      B b = new A();
                      b.Inner();
              

              You can't call the DoIt method (because B doesn't contain a definition for DoIt), but you will still get:

              B:Innner
              A:Inner

              Becuase the value inside the variable b is still an instance of the A class so the system calls the "latest" version.

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              S Offline
              S Offline
              sunsher
              wrote on last edited by
              #6

              Thanks all for your help. I now have an insight on Inheritance. :)

              OriginalGriffO 1 Reply Last reply
              0
              • S sunsher

                Thanks all for your help. I now have an insight on Inheritance. :)

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                You're welcome!

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                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