Explain the difference between method overloading and method overriding in C#


In C#,

1. Method Overloading:

Definition: Method overloading occurs when two or more methods in the same class have the same name but differ in their parameters (either in number, type, or both). It is a way to create multiple methods with the same name but different functionality based on the parameters passed.

  • Overloading is determined at compile time (also called static polymorphism).
  • The return type of overloaded methods can be the same or different, but the method signature (name + parameters) must be different.

Example of Method Overloading:

csharp



using System; class Calculator { // Method with one parameter public int Add(int a) { return a + 10; } // Overloaded method with two parameters public int Add(int a, int b) { return a + b; } // Overloaded method with three parameters public int Add(int a, int b, int c) { return a + b + c; } } class Program { static void Main() { Calculator calc = new Calculator(); Console.WriteLine(calc.Add(5)); // Output: 15 Console.WriteLine(calc.Add(5, 10)); // Output: 15 Console.WriteLine(calc.Add(5, 10, 20)); // Output: 35 } }

In the above example, the method Add is overloaded three times. The method name is the same, but the parameter count differs each time.


2. Method Overriding:

Definition: Method overriding occurs when a method in a derived class has the same name, return type, and parameters as a method in the base class. The method in the derived class overrides the method in the base class to provide specific implementation.

  • Overriding is determined at runtime (also called dynamic polymorphism).
  • The method in the base class must be marked as virtual (or abstract), and the method in the derived class must be marked with the override keyword.

Example of Method Overriding:

csharp

using System; class Animal { // Virtual method in the base class public virtual void Speak() { Console.WriteLine("Animal speaks"); } } class Dog : Animal { // Overriding the Speak method in the derived class public override void Speak() { Console.WriteLine("Dog barks"); } } class Program { static void Main() { Animal myAnimal = new Animal(); myAnimal.Speak(); // Output: Animal speaks Dog myDog = new Dog(); myDog.Speak(); // Output: Dog barks Animal animalRef = new Dog(); animalRef.Speak(); // Output: Dog barks (runtime polymorphism) } }

In the example above:

  • The method Speak is defined as virtual in the Animal class, which allows it to be overridden in the Dog class.
  • The Speak method in the Dog class provides a new implementation, and this is known as method overriding.
  • Notice the behavior when we use an Animal reference (animalRef) to refer to a Dog object. The method is resolved at runtime (dynamic polymorphism), and the overridden method in the Dog class is called.

Key Differences:

FeatureMethod OverloadingMethod Overriding
DefinitionSame method name, different parametersSame method name and signature in base/derived classes
Binding TimeCompile-time (Static Polymorphism)Runtime (Dynamic Polymorphism)
InheritanceNo inheritance requiredMust involve inheritance (base and derived classes)
Keyword UsedNo special keyword requiredoverride in the derived class, virtual in the base class
Return TypeCan be different, but doesn't affect overloadingMust have the same return type in both base and derived classes

Summary:

  • Method Overloading is when multiple methods with the same name but different parameters exist in the same class.
  • Method Overriding is when a method in a derived class provides its own implementation for a method already defined in the base class.

Check Internet Speed using Python

 


how to join multiple tables in a database to combine data from different sources with example


Explain the difference between method overloading and method overriding in C#

In C#, 1. Method Overloading: Definition: Method overloading occurs when two or more methods in the same class have the same name but diff...

Best for you