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. Which one is overriding and which one is overloading

Which one is overriding and which one is overloading

Scheduled Pinned Locked Moved C#
question
11 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
    Arun kumar Gautam
    wrote on last edited by
    #1

    1. class a{ public int add(int x) { return x; } } 2. class b { public double add(int x, int y, int z) { return x+y+z; } } m little confused about overriding and overloading can anyone tells wht happn in the above methods is it overloading or overriding????

    K Z C A 4 Replies Last reply
    0
    • A Arun kumar Gautam

      1. class a{ public int add(int x) { return x; } } 2. class b { public double add(int x, int y, int z) { return x+y+z; } } m little confused about overriding and overloading can anyone tells wht happn in the above methods is it overloading or overriding????

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      Arun kumar Gautam wrote:

      it overloading or overriding????

      According to the code you have neither. Polymorphism (search for overridden methods)[^] Overloading[^] The first would require one class to subclass and have a method with the same name AND input parameters (and the virtual/override keywords in c#). The second requires two methods with the same name but different input parameters (these can be in the same class or inheritance hierarchy). If b subclassed a or vice versa add would be overloaded, but there would be no overriding. Perhaps you copied the code from the question down incorrectly?

      “Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
      “One of the greatest problems of our time is that many are schooled but few are educated”

      Sir Thomas More (1478 – 1535)

      1 Reply Last reply
      0
      • A Arun kumar Gautam

        1. class a{ public int add(int x) { return x; } } 2. class b { public double add(int x, int y, int z) { return x+y+z; } } m little confused about overriding and overloading can anyone tells wht happn in the above methods is it overloading or overriding????

        Z Offline
        Z Offline
        ZurdoDev
        wrote on last edited by
        #3

        Override is when a class will "override" or implement it's own version of a method in a parent class. Overload is when the same named function has different signatures. In your example, if both add functions were in the same class it would be overloaded.

        There are only 10 types of people in the world, those who understand binary and those who don't.

        1 Reply Last reply
        0
        • A Arun kumar Gautam

          1. class a{ public int add(int x) { return x; } } 2. class b { public double add(int x, int y, int z) { return x+y+z; } } m little confused about overriding and overloading can anyone tells wht happn in the above methods is it overloading or overriding????

          C Offline
          C Offline
          Clifford Nelson
          wrote on last edited by
          #4

          http://www.aspfree.com/c/a/C-Sharp/Overriding-versus-Overloading/[^]

          1 Reply Last reply
          0
          • A Arun kumar Gautam

            1. class a{ public int add(int x) { return x; } } 2. class b { public double add(int x, int y, int z) { return x+y+z; } } m little confused about overriding and overloading can anyone tells wht happn in the above methods is it overloading or overriding????

            A Offline
            A Offline
            Arun kumar Gautam
            wrote on last edited by
            #5

            but according to the concept of overloading the number of parameters and data type of parameters in two functions must have to be different and according to the concept of overriding the number of parameters and datatype of parameters must have to be same in two functions so i think if the number of parameters and data type of parameters are different of two functions than it must be the overloading so it doesnt matter whether these two function are in same class or in derived class

            K C 2 Replies Last reply
            0
            • A Arun kumar Gautam

              but according to the concept of overloading the number of parameters and data type of parameters in two functions must have to be different and according to the concept of overriding the number of parameters and datatype of parameters must have to be same in two functions so i think if the number of parameters and data type of parameters are different of two functions than it must be the overloading so it doesnt matter whether these two function are in same class or in derived class

              K Offline
              K Offline
              Keith Barrow
              wrote on last edited by
              #6

              Arun kumar Gautam wrote:

              but according to the concept of overloading the number of parameters and / or data type of parameters in two functions must have to be different

              You've missed other important characteristics: The method must be have the same name (for both overloading and overriding). Overloading happen when you have methods with different parameters, with or without a subclass/superclass but both methods need to be in the same class hierarchy (including the same class - which has a hierarchy of 1). Neither of your add methods do this as they they are in unrelated classes, so you don't have overloading. Overriding requires class hierarchy where a method supersedes (overrides) it's equivalent (i.e. having same name, parameters) in class higher up in the hierarchy. As you don't have any subclassing/super classing you can't have overriding in your example. So that takes us back to your original question. Either you have copied the code across incorrectly, or the questioner doesn't know what they are doing/has made a mistake (which is a real possibility, I'm not being facetious).

              “Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
              “One of the greatest problems of our time is that many are schooled but few are educated”

              Sir Thomas More (1478 – 1535)

              A 1 Reply Last reply
              0
              • K Keith Barrow

                Arun kumar Gautam wrote:

                but according to the concept of overloading the number of parameters and / or data type of parameters in two functions must have to be different

                You've missed other important characteristics: The method must be have the same name (for both overloading and overriding). Overloading happen when you have methods with different parameters, with or without a subclass/superclass but both methods need to be in the same class hierarchy (including the same class - which has a hierarchy of 1). Neither of your add methods do this as they they are in unrelated classes, so you don't have overloading. Overriding requires class hierarchy where a method supersedes (overrides) it's equivalent (i.e. having same name, parameters) in class higher up in the hierarchy. As you don't have any subclassing/super classing you can't have overriding in your example. So that takes us back to your original question. Either you have copied the code across incorrectly, or the questioner doesn't know what they are doing/has made a mistake (which is a real possibility, I'm not being facetious).

                “Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
                “One of the greatest problems of our time is that many are schooled but few are educated”

                Sir Thomas More (1478 – 1535)

                A Offline
                A Offline
                Arun kumar Gautam
                wrote on last edited by
                #7

                1. class a{ public int add(int x) { return x; } } 2. class b:a { public double add(int x, int y, int z) { return x+y+z; } } sorry now can u help me for clearing my doubt.... is it overloading or overriding...????

                K 1 Reply Last reply
                0
                • A Arun kumar Gautam

                  1. class a{ public int add(int x) { return x; } } 2. class b:a { public double add(int x, int y, int z) { return x+y+z; } } sorry now can u help me for clearing my doubt.... is it overloading or overriding...????

                  K Offline
                  K Offline
                  Keith Barrow
                  wrote on last edited by
                  #8

                  Arun kumar Gautam wrote:

                  class b:a

                  That makes all the difference. b.Add overloads a.add as:

                  • Both methods are available from type b.
                  • Both methods have the same name
                  • b.add has a different input signature

                  “Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
                  “One of the greatest problems of our time is that many are schooled but few are educated”

                  Sir Thomas More (1478 – 1535)

                  A 1 Reply Last reply
                  0
                  • A Arun kumar Gautam

                    but according to the concept of overloading the number of parameters and data type of parameters in two functions must have to be different and according to the concept of overriding the number of parameters and datatype of parameters must have to be same in two functions so i think if the number of parameters and data type of parameters are different of two functions than it must be the overloading so it doesnt matter whether these two function are in same class or in derived class

                    C Offline
                    C Offline
                    Clifford Nelson
                    wrote on last edited by
                    #9

                    Overloadings can be overridden, but that requires inheriting a class/interface that already has the overloadings defined. You can also overload without inheriting from another class. However to Override you need to inherit from another class that already has the method with a signature defined.

                    1 Reply Last reply
                    0
                    • K Keith Barrow

                      Arun kumar Gautam wrote:

                      class b:a

                      That makes all the difference. b.Add overloads a.add as:

                      • Both methods are available from type b.
                      • Both methods have the same name
                      • b.add has a different input signature

                      “Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
                      “One of the greatest problems of our time is that many are schooled but few are educated”

                      Sir Thomas More (1478 – 1535)

                      A Offline
                      A Offline
                      Arun kumar Gautam
                      wrote on last edited by
                      #10

                      thnks for answer it make my concept completely clear.... thnks for this

                      K 1 Reply Last reply
                      0
                      • A Arun kumar Gautam

                        thnks for answer it make my concept completely clear.... thnks for this

                        K Offline
                        K Offline
                        Keith Barrow
                        wrote on last edited by
                        #11

                        No Problem.

                        “Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
                        “One of the greatest problems of our time is that many are schooled but few are educated”

                        Sir Thomas More (1478 – 1535)

                        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