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. "new" to hide base class implementation - is it useless?

"new" to hide base class implementation - is it useless?

Scheduled Pinned Locked Moved C#
comtutorialquestion
14 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.
  • D Offline
    D Offline
    devvvy
    wrote on last edited by
    #1

    Take a simple example from here[^] The following example, try remove "new" from B.Yell, it makes no difference (except Compiler will bitch about it). Let me know if I am mistaken! class Program { static void Main(string[] args) { try { A ref1 = new A(); A ref2 = new B(); B ref3 = new B(); ref1.Y(); <-- A.Yell ref2.Y(); <-- B.Yell ref3.Y(); <-- Always B.Yell, regardless whether you decorated Yell() with "new" or not! (This makes the keyword useless) } catch (Exception Ex) { Console.WriteLine("Main.Exception - " + Ex.ToString()); } return; } class A { public void Yell() { Console.WriteLine("A.Yell"); } } class B : A { public new void Yell() { Console.WriteLine("B.Yell"); } } }

    dev

    K M L A 4 Replies Last reply
    0
    • D devvvy

      Take a simple example from here[^] The following example, try remove "new" from B.Yell, it makes no difference (except Compiler will bitch about it). Let me know if I am mistaken! class Program { static void Main(string[] args) { try { A ref1 = new A(); A ref2 = new B(); B ref3 = new B(); ref1.Y(); <-- A.Yell ref2.Y(); <-- B.Yell ref3.Y(); <-- Always B.Yell, regardless whether you decorated Yell() with "new" or not! (This makes the keyword useless) } catch (Exception Ex) { Console.WriteLine("Main.Exception - " + Ex.ToString()); } return; } class A { public void Yell() { Console.WriteLine("A.Yell"); } } class B : A { public new void Yell() { Console.WriteLine("B.Yell"); } } }

      dev

      K Offline
      K Offline
      Keld Olykke
      wrote on last edited by
      #2

      Why would you expect A.Yell to be called when you have a reference to B? ...or did you mean to place your "Always B.Yell..." comment at ref2? Kind Regards, Keld Ølykke

      D 1 Reply Last reply
      0
      • K Keld Olykke

        Why would you expect A.Yell to be called when you have a reference to B? ...or did you mean to place your "Always B.Yell..." comment at ref2? Kind Regards, Keld Ølykke

        D Offline
        D Offline
        devvvy
        wrote on last edited by
        #3

        Keld Ølykke wrote:

        Why would you expect A.Yell to be called when you have a reference to B?

        Then what good is decorating the method B.Yell with "new" keyword? In the example, it does absolutely nothing. (i.e. remove it, you still have same result)

        dev

        K 1 Reply Last reply
        0
        • D devvvy

          Take a simple example from here[^] The following example, try remove "new" from B.Yell, it makes no difference (except Compiler will bitch about it). Let me know if I am mistaken! class Program { static void Main(string[] args) { try { A ref1 = new A(); A ref2 = new B(); B ref3 = new B(); ref1.Y(); <-- A.Yell ref2.Y(); <-- B.Yell ref3.Y(); <-- Always B.Yell, regardless whether you decorated Yell() with "new" or not! (This makes the keyword useless) } catch (Exception Ex) { Console.WriteLine("Main.Exception - " + Ex.ToString()); } return; } class A { public void Yell() { Console.WriteLine("A.Yell"); } } class B : A { public new void Yell() { Console.WriteLine("B.Yell"); } } }

          dev

          M Offline
          M Offline
          markovl
          wrote on last edited by
          #4

          I am pretty sure that the sole purpose of the new keyword in this context, is to hide the warning and has no other effect. And to back it up :) :

          MSDN[^] wrote:

          When used as a modifier, the new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. Although you can hide members without the use of the new modifier, the result is a warning. If you use new to explicitly hide a member, it suppresses this warning and documents the fact that the derived version is intended as a replacement.

          2A

          D 1 Reply Last reply
          0
          • D devvvy

            Keld Ølykke wrote:

            Why would you expect A.Yell to be called when you have a reference to B?

            Then what good is decorating the method B.Yell with "new" keyword? In the example, it does absolutely nothing. (i.e. remove it, you still have same result)

            dev

            K Offline
            K Offline
            Keld Olykke
            wrote on last edited by
            #5

            If you mean to call a base class method by inheritance there is 2 options: 1) inherited class does not override or new a base class method => B.Yell() will call A.Yell() in all cases 2) inherited class does override or new a base class method => B.Yell() has the full responsibility for calling base.Yell() You're example is not supposed to call base method, since you are missing a base.Yell() call in B - and it has nothing to do with the new keyword. Kind Regards, Keld Ølykke

            D 1 Reply Last reply
            0
            • D devvvy

              Take a simple example from here[^] The following example, try remove "new" from B.Yell, it makes no difference (except Compiler will bitch about it). Let me know if I am mistaken! class Program { static void Main(string[] args) { try { A ref1 = new A(); A ref2 = new B(); B ref3 = new B(); ref1.Y(); <-- A.Yell ref2.Y(); <-- B.Yell ref3.Y(); <-- Always B.Yell, regardless whether you decorated Yell() with "new" or not! (This makes the keyword useless) } catch (Exception Ex) { Console.WriteLine("Main.Exception - " + Ex.ToString()); } return; } class A { public void Yell() { Console.WriteLine("A.Yell"); } } class B : A { public new void Yell() { Console.WriteLine("B.Yell"); } } }

              dev

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The main intent of the new keyword in C# in this context is to let programmers and reviews know that this method is not an override and 'shadows' the base class method. This is specially targeted towards C++ programmers who migrated to C#. Remember, C++ does not have the override keyword. Thew new keyword also enables you to 'shadow' or 'redefine' a virtual method in a derived class. IMO, this was not possible in C++. Correct me if I'm wrong.

              1 Reply Last reply
              0
              • D devvvy

                Take a simple example from here[^] The following example, try remove "new" from B.Yell, it makes no difference (except Compiler will bitch about it). Let me know if I am mistaken! class Program { static void Main(string[] args) { try { A ref1 = new A(); A ref2 = new B(); B ref3 = new B(); ref1.Y(); <-- A.Yell ref2.Y(); <-- B.Yell ref3.Y(); <-- Always B.Yell, regardless whether you decorated Yell() with "new" or not! (This makes the keyword useless) } catch (Exception Ex) { Console.WriteLine("Main.Exception - " + Ex.ToString()); } return; } class A { public void Yell() { Console.WriteLine("A.Yell"); } } class B : A { public new void Yell() { Console.WriteLine("B.Yell"); } } }

                dev

                A Offline
                A Offline
                Abhinav S
                wrote on last edited by
                #7

                The new keyword does not make a difference in the case you are looking at. A bigger difference is made in the line A ref2 = new B(); If you remove new and use the override keyword, your output will be A.Yell B.Yell This article[^] explains the difference quite clearly.

                WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

                D 1 Reply Last reply
                0
                • M markovl

                  I am pretty sure that the sole purpose of the new keyword in this context, is to hide the warning and has no other effect. And to back it up :) :

                  MSDN[^] wrote:

                  When used as a modifier, the new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. Although you can hide members without the use of the new modifier, the result is a warning. If you use new to explicitly hide a member, it suppresses this warning and documents the fact that the derived version is intended as a replacement.

                  2A

                  D Offline
                  D Offline
                  devvvy
                  wrote on last edited by
                  #8

                  Great! Thanks Markovl!

                  dev

                  1 Reply Last reply
                  0
                  • K Keld Olykke

                    If you mean to call a base class method by inheritance there is 2 options: 1) inherited class does not override or new a base class method => B.Yell() will call A.Yell() in all cases 2) inherited class does override or new a base class method => B.Yell() has the full responsibility for calling base.Yell() You're example is not supposed to call base method, since you are missing a base.Yell() call in B - and it has nothing to do with the new keyword. Kind Regards, Keld Ølykke

                    D Offline
                    D Offline
                    devvvy
                    wrote on last edited by
                    #9

                    i know how to call the base class method - i am asking if "new" is useless, that its own relevance is to silence compiler warning. Read this.[^]

                    dev

                    K 1 Reply Last reply
                    0
                    • A Abhinav S

                      The new keyword does not make a difference in the case you are looking at. A bigger difference is made in the line A ref2 = new B(); If you remove new and use the override keyword, your output will be A.Yell B.Yell This article[^] explains the difference quite clearly.

                      WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

                      D Offline
                      D Offline
                      devvvy
                      wrote on last edited by
                      #10

                      Abhinav S wrote:

                      A bigger difference is made in the line A ref2 = new B();

                      You are wrong, I suggest you try it yourself. As pointed out by Markovl and myself this post, removing "new" makes no difference besides silencing compiler warning. Also, check out MSDN:[^] When used as a modifier, the new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. Although you can hide members without the use of the new modifier, the result is a warning. If you use new to explicitly hide a member, it suppresses this warning and documents the fact that the derived version is intended as a replacement. This is just another useless facility only relevant in enterprise interviews.

                      dev

                      A 1 Reply Last reply
                      0
                      • D devvvy

                        i know how to call the base class method - i am asking if "new" is useless, that its own relevance is to silence compiler warning. Read this.[^]

                        dev

                        K Offline
                        K Offline
                        Keld Olykke
                        wrote on last edited by
                        #11

                        Okay. I was in doubt about what you wanted to happen in your example. I have a problem with your word useless. I think you refer to compiled code not changing behavior or something similar. If so, I disagree with that opinion. In C# great efforts has been made to avoid that the programmer by accident does something he didn't mean to. One way that this is done, is by the introduction of more keywords e.g. virtual+override or new. This attitude from the language design team makes C# my favorite programming language. In short, no, I don't find it useless, and, yes, it seems to "just" hide a compiler warning :) Kind Regards, Keld Ølykke

                        D 1 Reply Last reply
                        0
                        • D devvvy

                          Abhinav S wrote:

                          A bigger difference is made in the line A ref2 = new B();

                          You are wrong, I suggest you try it yourself. As pointed out by Markovl and myself this post, removing "new" makes no difference besides silencing compiler warning. Also, check out MSDN:[^] When used as a modifier, the new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. Although you can hide members without the use of the new modifier, the result is a warning. If you use new to explicitly hide a member, it suppresses this warning and documents the fact that the derived version is intended as a replacement. This is just another useless facility only relevant in enterprise interviews.

                          dev

                          A Offline
                          A Offline
                          Abhinav S
                          wrote on last edited by
                          #12

                          I guess you are missing the whole point behind new. I mentioned this link[^] in my previous answer - am mentioning it again here - http://msdn.microsoft.com/en-us/library/ms173153%28v=vs.80%29.aspx[^]. The example clearly explains what new can do and what override does.

                          WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

                          D 1 Reply Last reply
                          0
                          • K Keld Olykke

                            Okay. I was in doubt about what you wanted to happen in your example. I have a problem with your word useless. I think you refer to compiled code not changing behavior or something similar. If so, I disagree with that opinion. In C# great efforts has been made to avoid that the programmer by accident does something he didn't mean to. One way that this is done, is by the introduction of more keywords e.g. virtual+override or new. This attitude from the language design team makes C# my favorite programming language. In short, no, I don't find it useless, and, yes, it seems to "just" hide a compiler warning :) Kind Regards, Keld Ølykke

                            D Offline
                            D Offline
                            devvvy
                            wrote on last edited by
                            #13

                            Thank you Keld, I was just looking for some sort of confirmation.

                            dev

                            1 Reply Last reply
                            0
                            • A Abhinav S

                              I guess you are missing the whole point behind new. I mentioned this link[^] in my previous answer - am mentioning it again here - http://msdn.microsoft.com/en-us/library/ms173153%28v=vs.80%29.aspx[^]. The example clearly explains what new can do and what override does.

                              WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

                              D Offline
                              D Offline
                              devvvy
                              wrote on last edited by
                              #14

                              i didn't miss anything Abhinav, (a) I wasn't asking about 'override' or polymorphism (b) *new* in context of method hiding really does nothing except to silence compiler warning

                              dev

                              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