Home > Article > Backend Development > Overloading and Overriding in C#
Polymorphism is one of the important concepts in C#. There are two types of polymorphism, compile time and run time. Overloading and Overriding concepts are used to achieve this respectively. In overriding, a child class can implement the parent class method in a different way but the child class method has the same name and same method signature as parent whereas in overloading there are multiple methods in a class with the same name and different parameters.
The working of overriding and overloading in C# are explained below with examples:
There are some keywords which we use in overriding like virtual, override and base.
Syntax:
class Parent { public virtual void Example() // base class { Console.WriteLine("parent class"); } } class Child: Parent { public override void Example() // derived class { base.Example(); Console.WriteLine("Child class"); } }
In this, virtual and override keywords are used which means the base class is virtual and child class can implement this class and override means this child class has the same name and same method signature as parent class.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverridingExample { class Subject // base class { public virtual void study() // base class method { Console.WriteLine("Study all the subjects"); } } class Mathematics: Subject // derived class { public override void study() // derived class method { Console.WriteLine("Study Mathematics"); } } class Program { // main method static void Main(string[] args) { Subject s = new Mathematics(); s.study(); Console.ReadLine(); } } }
In the above example, the methods name is the same but their implementation is different. The base class has virtual and due to that child class can implement parent class method in its own way. The child class method has keyword override which shows that this method is an override method.
Output:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverridingExample { class Subject // base class { public virtual void study() // base class method { Console.WriteLine("Study all the subjects"); } } class Mathematics: Subject // derived class { public override void study() // derived class method { base.study(); Console.WriteLine("Study Mathematics"); } } class Program { // main method static void Main(string[] args) { Mathematics m = new Mathematics(); m.study(); Console.ReadLine(); } } }
Output:
In this example, the derived class has a base keyword that is used to call the base class method. So, in that case, the derived method is called after the base class method.
Points to Remember:
In overloading, there are multiple methods with different method signatures. Below are some examples that show how we can achieve overloading by varying the number of parameters, the order of parameters, and data types of parameters.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverloadingExample { class Demo { public int Sum(int x, int y) { int value = x + y; return value; } public int Sum(int x, int y, int z) { int value = x + y + z; return value; } public static void Main(string[] args) // main method { Demo d = new Demo(); int sum1 = d.Sum(24, 28); Console.WriteLine("sum of the two " + "integer value : " + sum1); int sum2 = d.Sum(10, 20, 30); Console.WriteLine("sum of the three " + "integer value : " + sum2); Console.ReadLine(); } } }
In the above example, there are two methods with the same name but a different number of parameters. The first method consists of two parameters while the second one has three parameters. This is called method overloading.
Output:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverloadingExample { class Demo { public int Sum(int x, int y, int z) { int value = x + y + z; return value; } public double Sum(double x, double y, double z) { double value = x + y + z; return value; } public static void Main(string[] args) // main method { Demo d = new Demo(); int sum1 = d.Sum(24, 28,7); Console.WriteLine("sum of the two " + "integer value : " + sum1); double sum2 = d.Sum(10.0, 20.0, 30.0); Console.WriteLine("sum of the three " + "integer value : " + sum2); Console.ReadLine(); } } }
In the above example, there are two methods with the same name but their data types are different. The first method has an integer data type while the second has a double data type. So in this case parameters are varying because of the different datatype.
Output:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverloadingExample { class Demo { public void Details(String name,int id) { Console.WriteLine("Name " + name + ", " + "Id " + id); ; } public void Details(int id,string name) { Console.WriteLine("Name " + name + ", " + "Id " + id); } public static void Main(string[] args) // main method { Demo d = new Demo(); d.Details("John", 10); d.Details("Joe", 20); Console.ReadLine(); } } }
In the above example, the name of the methods are the same but the order of parameters are different. The first method has a name and id resp. whereas the second one has id and name respectively.
Output:
Points to Remember:
Following are the advantages explained.
Overloading and overriding play a major role in achieving polymorphism. Overriding allows derived class to implement in its own way and on the other hand overloading is about methods with the same name and various types of parameter implementations.
The above is the detailed content of Overloading and Overriding in C#. For more information, please follow other related articles on the PHP Chinese website!