

Overloading Vs. Overriding in C#
source link: https://hackernoon.com/overloading-vs-overriding-in-c-nn1331h4?source=rss
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Overloading Vs. Overriding in C#

















@samwalpoleSam Walpole
Fullstack .NET and JavaScript web developer. Coding teacher and advocate
Method overloading and overriding are two common forms of polymorphism ( the ability for a method or class to have multiple forms) in C# that are often confused because of their similar sounding names. In this article, we show the difference between the two with some practical code examples.
Overloading
Overloading is the ability to have multiple methods within the same class with the same name, but with different parameters. Each of these methods has their own implementation as well, meaning that they can behave differently depending on what is passed in.
Overloading is known as compile-time (or static) polymorphism because each of the different overloaded methods is resolved when the application is compiled.
To demonstrate overloading, lets start with a calculator class that doesn't use overloading:
public class Calculator
{
public int AddTwoInt(int a, int b) => a + b;
public int AddThreeInt(int a, int b, int c) => a + b +c;
public double AddTwoDouble(double a, double b) => a + b;
public double AddThreeDouble(double a, double b, double c) => a + b + c;
}
Here we 4 different methods each with completely different names, each of which performs basically the same the same function, just on different types and amounts of parameters. It's a pretty confusing interface to deal with because you've got to remember the name and parameters for each different method.
Instead, we could refactor this class to use overloading:
public class Calculator
{
public int Add(int a, int b) => a + b;
public int Add(int a, int b, int c) => a + b +c;
public double Add(double a, double b) => a + b;
public double Add(double a, double b, double c) => a + b + c;
}
Now all of the methods are just called Add, which is a much simpler interface to deal with as a programmer. We can simply call the Add method with the parameters that we have, and the compiler will automatically work out for us which actual implementation to use!
var calc = new Calculator();
int a = 5;
int b = 6;
// calls int Add(int a, int b)
calc.Add(a, b);
double c = 4.2
double d = 5.41
double e = 2.57
// calls double Add(double a, double b, double c)
calc.Add(c, d, e);
Overriding
Overriding, on the other hand, is the ability to redefine the implementation of a method in a class that inherits from a parent class. When a method is overridden, the name and the parameters stay the same, but the implementation that gets called depends on the type of the object that's calling it.
Overriding is known as runtime (or dynamic) polymorphism because the type of the calling object is not known until runtime, and therefore the method implementation that runs is determined at runtime.
As an example, imagine we have a base class called Dog with a Woof method. We mark the method as virtual to signify that this method can be overriden by inheriting classes.
public class Dog
{
public virtual void Woof()
{
Console.WriteLine("Woof!");
}
}
Currently this and any inherited classes will use the Woof method defined in the Dog class:
public class BigDog : Dog
{
}
var dog = new Dog();
var bigDog = new BigDog();
dog.Woof() // prints Woof!
bigDog.Woof() // prints Woof!
However, we can choose to override the method so that our inherited classes have a different implementation of Woof:
public class YappyDog : Dog
{
public override void Woof()
{
Console.WriteLine("Woof! Woof! Woof!");
}
}
var dog = new Dog();
var yappyDog = new YappyDog();
dog.Woof() // prints Woof!
yappyDog.Woof() // prints Woof! Woof! Woof!
Here the base Dog class still uses it's own implementation, but the inherited YappyDog has it's own overridden implementation that it uses. The application checks at runtime what the type of the class is (Dog or YappyDog) and calls the method for that particular type.
Conclusion
Here we have compared two forms of polymorphism in C#, overloading and overriding. We have seen that:
- Overloading is determined at compile time and is static. Overriding is determined at runtime and is dynamic.
- Overloading concerns giving a method with the same name different parameters. Overriding concerns defining a different implementation of the same method in inherited classes.
Since it's almost Christmas, you could also check out my Top Christmas Gifts To Buy A Developer post that was featured on Hashnode recently.
I post mostly about full stack .NET and Vue web development. To make sure that you don't miss out on any posts, please follow this blog and subscribe to my newsletter. If you found this post helpful, please like it and share it. You can also find me on Twitter.
Previously published at https://samwalpole.com/the-difference-between-overloading-and-overriding-in-csharp
















Create your free account to unlock your custom reading experience.
Recommend
-
62
上下文假如我们项目中引入的三方包出现了一个小的bug,我们通常会fixed它,然后创建一个pull request,但是通常我们等不到pull request被merge之后的release版本。这种现象非常普遍,对于这种情况,我们通过有如下三种方案: 使用merge了pull request的snapshot版...
-
37
I’ve been programming in Java for over half a decade, and thought I knew how overloading and overriding worked. It was only once I started thinki...
-
39
I've been programming in Java for over half a decade, and thought I knew how overloading and overriding worked. It was only once I started thinking of and writing up the following corner cases, that I realized I didn't know it nearly as well as...
-
22
When a super class method (overridden method) declares that it can throw an exception then sub class method (overriding method) must also declare that it can throw the same kind of exception or a sub type of that...
-
10
Writing Cleaner View Code in Swift By Overriding loadView() Writing Cleaner View Code in Swift By Overriding loadView() July 23, 2018 ...
-
9
Overriding part of an Emacs themeApril 6, 2020IntroductionLike most sane people, I don’t use Emacs’ default theme but instead rely on custom themes.Through a thoughtful choice of colors, the venerable edito...
-
10
There are a variety of “buttons” in HTML. You’ve got: <button>Button</button> <input type="button" value="Button"> Plus, for better or worse, people like having links that are style...
-
10
哎呀妈呀,等老久了吧!关于 Java 方面的文章终于来了,快快快,扶寡人起来,还能再举——鼎(明眼人都能看的出来,我受大秦帝国之纵横天下这部剧的影响了)。 说回正题。重写(Overriding)算是 Java 中一个非常重要的概念,理解重写到底是什么对每个...
-
6
30 Sep 2019 Updated: 30 Sep 2019 software-dev ...
-
8
In Java, what’s the difference between method overloading and method overriding? The difference between overriding and overloading in Java is a common source of confusion – but it is fairly easy to understand with the examples we pr...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK